RSpec Simple Specifications

Posted by yrashk

Copied from Railsware#blog

What I also like in RSpec is that it constructs a name for simplistics specifications, like:

 
 context "Random number in 0..100 range" do 

  setup do
    @random_number = rand(100)
  end

  specify do
    @random_number.should < 100
  end

  specify do
    @random_number.should >= 0
  end

  specify do
    @random_number.should_not be_nil
  end

end

Random number in 0..100 range
- should < 100
- should >= 0
- should not be nil

Update:

I’ve played around matchers to make generated names work properly for be_ matchers (like be_between): http://blog.railsware.com/2007/3/2/rspec-generated-spec-name-for-be (and it’s accepted in rspec trunk now)

RSpec on Rails

Posted by yrashk

While I have discovered RSpec on Rails for a while ago, I haven’t really used it yet.

But today I have moved Frontcamp tests to RSpec specifications, finally. Even without test2spec (it failed to translate my tests for some reason), transition to specs did not took too much time. I’ve accomplished it in 2-3 hours or so.

What should I say? I like it. My tests were completely bad organized before, I’ve found that I’ve missed few bugs in an implementation and I’ve found that my test aren’t covering some important aspects. And for all this, thank you, RSpec team.

To not to change my habit to type `rake’ to run tests I’ve written very simple lib/tasks/rakespec.rake:

1
2
3
task :default do
        Rake::Task["spec"].invoke
end

Also, I’m very excited about `spec -f s’ and `spec -f h’ output.

I have been warned that running tests without rails_spec_server (now it seems to be called rails_spec_runner) could be not very fast. But since I’ve found that I need to restart rails_spec_runner mostly after each rails_spec invocation (probably some bug, or may be I’m missing something), I’ve tried to run `spec’ and I’ve found its speed reasonable and acceptable.

More to report later. Anyway, my specifications are in a trunk already.