[rspec-users] Can some one please explain why one of those two examples fails?

Michael Kintzer rockrep at gmail.com
Thu Mar 15 14:53:56 UTC 2012


In your first example, last_token is declared in a before block, which means you need to mark it as an instance variable of the ExampleGroup '@' to reference it in the examples within the context.   You don't need to do that when you use let.

Also you don't need to declare the redundant let creating a user in that first context, since it was declared outside of the context already.

HTH

Michael

On Mar 13, 2012, at 4:55 PM, Mohamad El-Husseini <husseini.mel at gmail.com> wrote:

> The following are what I believe two ways of doing the same thing. Only the first example fails, while the latter passes. I'm finding it hard to get to understand describe and context blocks, particularly with respect to scopes. I would appreciate any "for dummies" explanation, or a link to a blog post that can clear this up for me. I'm still new to Rails and Ruby.
> 
>   describe "send password reset" do
>     let(:user) { FactoryGirl.create(:user) }
> 
>     # This fails
>     context "generates a unique password_reset_token each time" do
>       let(:user) { FactoryGirl.create(:user) }
>       before do
>         user.send_password_reset
>         last_token = user.password_reset_token
>         user.send_password_reset
>       end
>       its(:password_reset_token) { should_not == last_token }
>     end
> 
>     # This passes
>     it "generates a unique password_reset_token each time" do
>       user.send_password_reset
>       last_token = user.password_reset_token
>       user.send_password_reset
>       user.password_reset_token.should_not == last_token
>     end
>   end
> 
> The first example fails with this:
> 
> Failure/Error: its(:password_reset_token) { should_not == last_token }
>      NameError:
>        undefined local variable or method `last_token' for #<RSpec::Core::ExampleGroup::Nested_6::Nested_7::Nested_1::Nested_1:0x0000000561e130>
> 
> Earlier, it out puts this:
> 
>  should not When you call a matcher in an example without a String, like this:
> 
> specify { object.should matcher }
> 
> or this:
> 
> it { should matcher }
> 
> RSpec expects the matcher to have a #description method. You should either
> add a String to the example this matcher is being used in, or give it a
> description method. Then you won't have to suffer this lengthy warning again.
>  (FAILED - 1)
> 
> 
> Another thing I noticed is that I can not use capybara inside of describe blocks unless the calls are in a before block... but I don't understand why.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list