Wednesday, May 03, 2006

Dave Astels makes some RSpec revisions

Canada on Rails was a great chance to meet some new people and pick up some more Ruby and Rails tidbits.

Dave Astels gave a presentation on the state of RSpec, which seems to be coming right along. They've made some major revisions since my previous post back in October to provide some very nice syntactic sugar. I think he's spot on with most of the changes, but I agree with what he's said about changing "should" to "must."

If you missed his presentation in Vancouver, here's a similar one on google video.

To compare the old way with the new way, here is what the spec from my earlier post would look like now.

movie_list_spec.rb:
require 'spec' 
require 'movie'
require 'movie_list'

context "An empty movie list" do

setup do
@movie_list = MovieList.new
end

specify "should have a size of zero" do
@movie_list.size.should.be 0
end

specify "should not include the movie 'Star Wars'" do
@movie_list.should.not.include "Star Wars"
end

end

context "A movie list with one movie" do

setup do
@movie_list = MovieList.new
@movie_list.add Movie.new("Star Wars")
end

specify "should have a size of one" do
@movie_list.size.should.be 1
end

specify "should include the movie 'Star Wars'" do
@movie_list.should.include "Star Wars"
end

specify "should not include the movie 'Serenity'" do
@movie_list.should.not.include "Serenity"
end

end


As you can see, they've done away with the class and method definitions, in favor or a more expressive DSL with methods like 'context', 'setup', and 'specify' to which you pass in a block of assertions. And they've removed the underscore named methods with a method chain ('should.not.be', 'should.not.include') that, while maybe a bit unrubish, you could argue reads a bit more naturally - in English, anyway.

Dave has also said that they will be porting RSpec to Java and C# at some point, though it won't be quite as pretty an API as Ruby. ;)

0 Comments:

Post a Comment

<< Home