[rspec-users] Can you explain this code to me?

Pat Maddox pergesu at gmail.com
Thu Feb 7 19:31:13 EST 2008


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


More information about the rspec-users mailing list