views:

123

answers:

2

I was looking through the Datamapper directories and opened up dm-core/tasks/dm.rb. Just generally speaking, what the heck is going on in this file? It looks like Greek to me. Particularly this thing about "specs" - what are those for? Is that similar to a software spec which defines what a project is supposed to encompass?

require 'spec/rake/spectask'
require 'spec/rake/verify_rcov'

task :default => 'spec'

RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
  t.threshold = 87.7 # Make sure you have rcov 0.7 or higher!
end

def run_spec(name, files, rcov)
  Spec::Rake::SpecTask.new(name) do |t|
    t.spec_opts << '--options' << ROOT + 'spec/spec.opts'
    t.spec_files = Pathname.glob(ENV['FILES'] || files.to_s).map { |f| f.to_s }
    t.rcov = rcov
    t.rcov_opts << '--exclude' << 'spec'
    t.rcov_opts << '--text-summary'
    #t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
    #t.rcov_opts << '--only-uncovered'
    #t.rcov_opts << '--profile'
  end
end

public_specs     = ROOT + 'spec/public/**/*_spec.rb'
semipublic_specs = ROOT + 'spec/semipublic/**/*_spec.rb'
all_specs        = ROOT + 'spec/**/*_spec.rb'

desc 'Run all specifications'
run_spec('spec', all_specs, false)

desc 'Run all specifications with rcov'
run_spec('rcov', all_specs, true)

namespace :spec do
  desc 'Run public specifications'
  run_spec('public', public_specs, false)

  desc 'Run semipublic specifications'
  run_spec('semipublic', semipublic_specs, false)
end

namespace :rcov do
  desc 'Run public specifications with rcov'
  run_spec('public', public_specs, true)

  desc 'Run semipublic specifications with rcov'
  run_spec('semipublic', semipublic_specs, true)
end

desc 'Run all comparisons with ActiveRecord'
task :perf do
  sh ROOT + 'script/performance.rb'
end

desc 'Profile DataMapper'
task :profile do
  sh ROOT + 'script/profile.rb'
end
A: 

Yes.

Spec files contain the specifications for behavior driven development. Check out rspec.

eta: I just read your question a bit more thoroughly. The file you're looking at is a rake file. It's calling on spec to run your BDD specifications.

Trevor Oke
+3  A: 

What you actually have there is a rake file that calls the rspec tests. The actual specs would be in files named foo_spec.rb and are a lot more readable.

RSpec is a framework for Behaviour Driven Development (BDD) and is used as an alternative to testunit the traditional Unit Testing framework in Ruby.

The real benefit to using BDD over traditional unit testing is one of having readable tests that quite literally read as specs.

I often sit with non technical clients and read through the spec source file to see if they make sense to them or if there are any rules missing. In almost all cases they can follow them intelligently.

Here is a silly simple example:

describe User do
  describe "basic generation" do
    before(:each) do
      @user=User.create :first_name=>"Bob, :last_name=>"Smith"
    end

    it "should be valid" do
      @user.should be_valid
    end

    it "should have a full name" do
      @user.full_name.should=="Bob Smith"
    end
  end
end

As the other poster said, go to the RSpec web site for more info.

Pelle