[rspec-users] should_be_different -- possible implementation
s.ross
cwdinfo at gmail.com
Mon Dec 4 12:27:58 EST 2006
There is an assert_difference helper for Test::Unit (you can read
about it at http://blog.caboo.se/articles/2006/06/13/a-better-
assert_difference and to some it should look familiar -- you know who
you are :), so I took a shot at one for rSpec on Rails.
class Object
def should_be_different(method = nil, difference = nil)
return self.should_not_eql(yield) if method.nil?
before = self.send(method)
yield
return self.send(method).should_not_eql(before) if
difference.nil?
(self.send(method) - before).should_eql difference
end
end
Usage:
specify "should be able to create a new user" do
Person.create(:name => 'joe')
Person.should_be_different :count do
Person.create(:name => 'bob')
end
end
This has probably already been done, but I thought I'd put it out
here for comment in case it hadn't. Note that if you omit the method,
the objects are tested for equality (however the object defines it).
If you include the method but omit the difference, then any amount
difference is ok. Testing destroy might indicate using -1 for the
difference to indicate that the count has gone down.
My implementation works, but a more natural syntax would be:
Person.count.should_be_different do
blah
end
and:
Person.count.should_be_different_by 1 do
blah, blah
end
Comments?
Steve
More information about the rspec-users
mailing list