I am experimenting with the restful-authentication plug in and I see that it has generated an rspec encantation which I had not seen before: it ''allows signup'' do lambda do create_user response.should be_redirect end.should change(User, :count).by(1) end What does the combination of a lambda and a .should mean? Similarly but a little different: describe ''being created'' do before do @user = nil @creating_user = lambda do @user = create_user violated "#{@user.errors.full_messages.to_sentence}" if @user.new_record? end end it ''increments User#count'' do @creating_user.should change(User, :count).by(1) end end Thanks for any pointers or clarifications!
On Feb 7, 2008 5:26 PM, Pito Salas <rps at salas.com> wrote:> I am experimenting with the restful-authentication plug in and I see > that it has generated an rspec encantation which I had not seen > before: > > it ''allows signup'' do > lambda do > create_user > response.should be_redirect > end.should change(User, :count).by(1) > end > > What does the combination of a lambda and a .should mean?This should answer your question: http://rspec.info/rdoc/classes/Spec/Matchers.html#M000386 HTH, David> > Similarly but a little different: > > describe ''being created'' do > before do > @user = nil > @creating_user = lambda do > @user = create_user > violated "#{@user.errors.full_messages.to_sentence}" if > @user.new_record? > end > end > > it ''increments User#count'' do > @creating_user.should change(User, :count).by(1) > end > end > > Thanks for any pointers or clarifications! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Feb 7, 2008 9:26 PM, Pito Salas <rps at salas.com> wrote:> What does the combination of a lambda and a .should mean?I''m new to the list, but I think I can answer that :).>From what I know, it means that RSpec will run the lambda''s code andcheck that it had the effect specified in the "should" call. And from that I can determine by looking at the two incantations you posted, the lambda is used because the code can throw an error, and the spec''s author didn''t want the first error to prevent the outer "should" from being checked. -- Bira http://compexplicita.wordpress.com http://compexplicita.tumblr.com
On Feb 7, 2008 3:40 PM, Bira <u.alberton at gmail.com> wrote:> On Feb 7, 2008 9:26 PM, Pito Salas <rps at salas.com> wrote: > > > What does the combination of a lambda and a .should mean? > > I''m new to the list, but I think I can answer that :). > > >From what I know, it means that RSpec will run the lambda''s code and > check that it had the effect specified in the "should" call. > > And from that I can determine by looking at the two incantations you > posted, the lambda is used because the code can throw an error, and > the spec''s author didn''t want the first error to prevent the outer > "should" from being checked.Sometimes, but in this case, what you are trying to do is before_count = User.send :count create_user response.should be_redirect User.send(:count).should == before_count + 1 that''s some boilerplate that can be abstracted away... def change(target, method) before_count = target.send method yield target.send(method).should == before_count + 1 end (not exactly how it''s implemented, but it should give you a rough idea of what''s going on) Pat
On 8.2.2008, at 1.26, Pito Salas wrote:> I am experimenting with the restful-authentication plug in and I see > that it has generated an rspec encantation which I had not seen > before: > > it ''allows signup'' do > lambda do > create_user > response.should be_redirect > end.should change(User, :count).by(1) > end > > What does the combination of a lambda and a .should mean?Lambda is an anonymous function, or pretty much the same as a block of code. So in the case above, running the code inside the lambda block should change the user count by one. This is the same as assert_difference does in test/unit.> > > Similarly but a little different: > > describe ''being created'' do > before do > @user = nil > @creating_user = lambda do > @user = create_user > violated "#{@user.errors.full_messages.to_sentence}" if > @user.new_record? > end > end > > it ''increments User#count'' do > @creating_user.should change(User, :count).by(1) > end > end > > Thanks for any pointers or clarifications!Same thing, just this time the function is first stored in an instance variable. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
Thanks all for excellent pointers! On Feb 8, 2008 2:21 AM, Jarkko Laine <jarkko at jlaine.net> wrote:> On 8.2.2008, at 1.26, Pito Salas wrote: > > > I am experimenting with the restful-authentication plug in and I see > > that it has generated an rspec encantation which I had not seen > > before: > > > > it ''allows signup'' do > > lambda do > > create_user > > response.should be_redirect > > end.should change(User, :count).by(1) > > end > > > > What does the combination of a lambda and a .should mean? > > Lambda is an anonymous function, or pretty much the same as a block of > code. So in the case above, running the code inside the lambda block > should change the user count by one. This is the same as > assert_difference does in test/unit. > > > > > > > Similarly but a little different: > > > > describe ''being created'' do > > before do > > @user = nil > > @creating_user = lambda do > > @user = create_user > > violated "#{@user.errors.full_messages.to_sentence}" if > > @user.new_record? > > end > > end > > > > it ''increments User#count'' do > > @creating_user.should change(User, :count).by(1) > > end > > end > > > > Thanks for any pointers or clarifications! > > Same thing, just this time the function is first stored in an instance > variable. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >