Mar 01

rSpec 0.4.0.. New & Improved!

dastels @ 6:39 am

Steve Baker, David Chelimsky, and I are pleased to announce that a new version of the rspec Gem was released today. Get it by:

gem install rspec

The changes are work that David Chelimsky and I have been working on over the past few weeks (hence the “two Daves walked into a bar” release… Steve is sooo funny). Steve merged our code back into the trunk and released a gem today. The change is the way expectations are done. Previously the expectation methods were added directly to Object. For example, should_be_nil, should_include, and should_be_empty.

The first change is that we now only add one method to Object: should. This returns a ShouldHelper instance which is responsible for the rest of the expectation. Go see the code if you’re interested in the technical details.

The second is that things are more open-ended now. Previously you were limited to the expectations that were provided, unless you went totally open-ended (losing good failure messages in the process) and passed a block as an expectation. You can still do that (with should.satisfy {|target| ...}) but now several of the expectation forms support use of arbitrary predicates.

For example, if the target object responds to empty? you can use the form target.should.be.empty. This is equivalent to target.empty?.should.be true. Furthermore, you can pass arguments: player.should.be.facing :south where player.facing?(:south) returns whether the player is, indeed, facing south.

All of the earlier expectation forms are supported, but a bit differently. Instead of writing target.should_equal 5, you now say target.should.equal 5. Not much of a change.

A slight change has been made to should_be_true, should_be_false, and should_be_nil, These now use the generic identity expectation should.be. They are now, respectively should.be true, should.be false, and should.be nil.

Finally there are two major additions. First is support for class/type related expectations: target.should.be.an.instance.of Class, target.should.be.a.kind.of Class, and target.should.respond.to :message, which use instance_of?, kind_of?, and respond_to?, respectively.

The second new feature related to collection sizes. Well, actually it will work with anything that has a concept of size… responding to a size or length message by returning a count. Some examples will illustrate it best, I think:

car.should.have(4).wheels

car responds to wheels by returning something that is responsible for its wheels… specifically knowing how many (through the use of size or length). The expectation set is that when asked, this object will report 4 wheels.

car.should.have.at.least(3).wheels

Same idea as above, but the expectation is that the reported value will be no lower than 3.

car.should.have.at.most(6).wheels

Same idea as above, but the expectation is that the reported value will be no greater than 6.

That’s it I think. Everything else is the same as it was before (the Context, runners, mocking, etc). What we really need now is for people to use it and find anything we missed, or better ways of doing things.. before we lock the API down in release 1.0.



Steve Jobs Memorial

Stay Hungry, Stay Foolish!
Steve Jobs

In memory of Steve Jobs