RSpec Simple Specifications

Posted by yrashk on March 02, 2007

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
Comments
  1. Michael KlishinMarch 02, 2007 @ 03:00 AM

    Tiny addition that saves time and can force bad habits for larger specs :)

  2. David ChelimskyMarch 02, 2007 @ 01:38 PM

    Michael what bad habits are you concerned about?

    Of course, you don’t need to use that feature ;) But it is a feature that a LOT of people have wanted for a long time.

  3. Yurii RashkovskiiMarch 02, 2007 @ 01:41 PM

    David,

    that’s a really nice feature, and I like it. Although I’m trying to hack rspec to let it autogenerate proper names for more complex cases, like

    @random_number.should be_between(0,100)

  4. Michael KlishinMarch 02, 2007 @ 01:44 PM

    David,

    I know, count me in there. But for higher level testing that take place in Rails specification captions become a must. This is the only concern.

  5. David ChelimskyMarch 02, 2007 @ 04:51 PM

    Michael – I’m not clear on what you mean. Can you give a specific example of where using an auto-generated description would cause confusion?

    I’m not doubting you. I just don’t understand what you’re getting at.