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
David Chelimsky
2006-Dec-04 17:36 UTC
[rspec-users] should_be_different -- possible implementation
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 >
aslak hellesoy
2006-Dec-04 18:28 UTC
[rspec-users] should_be_different -- possible implementation
On 12/4/06, David Chelimsky <dchelimsky at gmail.com> wrote:> 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: >Typo: It''s called should_change> 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 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2006-Dec-04 18:33 UTC
[rspec-users] should_be_different -- possible implementation
On 12/4/06, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 12/4/06, David Chelimsky <dchelimsky at gmail.com> wrote: > > 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: > > > > Typo: It''s called should_changeLOL I got half way there.> > > 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 > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Did the syntax discussion ever get settled. The signature is should_change(receiver, message) but there were discussions in this thread: http://rubyforge.org/pipermail/rspec-devel/2006-November/001251.html That discussed lots of cool syntactic sugar. What was settled upon? Person.should_change :count do Person.create(:name => ''ozzy osbourne'') end How do you specify by how much? Thx On Dec 4, 2006, at 10:33 AM, David Chelimsky wrote:> On 12/4/06, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: >> On 12/4/06, David Chelimsky <dchelimsky at gmail.com> wrote: >>> 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: >>> >> >> Typo: It''s called should_change > > LOL > > I got half way there. > > >> >>> 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 >>>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2006-Dec-04 19:02 UTC
[rspec-users] should_be_different -- possible implementation
On 12/4/06, s.ross <cwdinfo at gmail.com> wrote:> Did the syntax discussion ever get settled.http://rspec.rubyforge.org/documentation/expectations.html> The signature is > should_change(receiver, message) but there were discussions in this > thread: > > http://rubyforge.org/pipermail/rspec-devel/2006-November/001251.html > > That discussed lots of cool syntactic sugar. What was settled upon? > > Person.should_change :count do > Person.create(:name => ''ozzy osbourne'') > end > > How do you specify by how much? > > Thx > > > On Dec 4, 2006, at 10:33 AM, David Chelimsky wrote: > > > On 12/4/06, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: > >> On 12/4/06, David Chelimsky <dchelimsky at gmail.com> wrote: > >>> 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: > >>> > >> > >> Typo: It''s called should_change > > > > LOL > > > > I got half way there. > > > > > >> > >>> 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 > >>>> > >>> _______________________________________________ > >>> rspec-users mailing list > >>> rspec-users at rubyforge.org > >>> http://rubyforge.org/mailman/listinfo/rspec-users > >>> > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > >> > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >