[rspec-users] should_be_different -- possible implementation
David Chelimsky
dchelimsky at gmail.com
Mon Dec 4 12:36:10 EST 2006
On 12/4/06, s.ross <cwdinfo at gmail.com> wrote:
> 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.
We've already got this in 0.7.4. It's called assert_change, but it's
the same functionality you're looking for:
http://rspec.rubyforge.org/documentation/expectations.html
Cheers,
David
>
>
> 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
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
More information about the rspec-users
mailing list