First, the test fails because it expects a Proc -- it's trying to
invoke #call. So you can't do what you're proposing without writing a
new definition for how #change works.

As a deeper answer, your "shouldn't these be equivalent?" statement is
not accurate. RSpec, like any piece of Ruby code, needs to know when
it should start paying attention for the change(). In other words, it
needs a "before" to start with and an "after" to compare to. With a
lambda, this is accomplished by measuring the thing you want to check
before the lambda is run, and then again after it's run. If the
difference matches what you expected, the test passes. If it doesn't
it fails.

Your example is asking RSpec to do this:

before.val = 5
before.val = 6
before.should change(:val).from(5).to(6)

How can it know that `before.val` used to be 5? Answer: it can't. By
the time you reach the third line, the old value of `before.val` is
gone forever.

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Mon, Jul 25, 2011 at 07:12, 7stud -- <[email protected]> wrote:
> Here is the code in question:
>
>
> describe UsersController do
>  render_views
>  …
>  …
>
>  describe "POST to 'create' action" do
>    describe "failure" do
>      before(:each) do
>        @attr = { :name => '', :email => '', :password => '',
>                         :password_confirmation => '' }
>      end
>
>      it "should not create a user" do
>        lambda {
>          post :create, :user => @attr
>        }.should_not change(User, :count)
>      end
>
>      it "should have the right title" do
>        post :create, :user => @attr
>        response.should have_selector(:title, :content => 'Sign up')
>      end
>
>
>
>
> Comparing the last two tests, it looks to me like:
>
>
>
>       lambda {
>          post :create, :user => @attr
>        }.should_not change(User, :count)
>
> should be equivalent to:
>
>        post :create, :user => @attr
>        response.should_not change(User, :count)
>
>
> But when I make that change, the test fails with this rror:
>
>
>
> Failures:
>
>  1) UsersController Post create failure should not create a user
>     Failure/Error: response.should_not change(User, :count)
>     NoMethodError:
>       undefined method `call' for
> #<ActionController::TestResponse:0x00000100f3d3a0>
>     # ./spec/controllers/users_controller_spec.rb:62:in `block (4
> levels) in <top (required)>'
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to