Forum: Ruby on Rails How to solve NameError: undefined local variable or method `user'

1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-06-03 19:01
Attachment: authentication_pages_spec.rb (4 KB)
I am using Ruby 1.9.3 and Rails 3.2.16.

When I run test suite at the end of section 11.2.4 as follow:

bundle exec rspec spec/

I get following error:

 1) Authentication authorization in the Users controller visiting the
following page
     Failure/Error: before { visit following_user_path(user) }
     NameError:
       undefined local variable or method `user' for
#<RSpec::Core::ExampleGroup::Nested_7::Nested_3::Nested_3::Nested_2:0x00000006402418>
     # ./spec/requests/authentication_pages_spec.rb:93:in `block (5
levels) in <top (required)>'

  2) Authentication authorization in the Users controller visiting the
followers page
     Failure/Error: before { visit followers_user_path(user) }
     NameError:
       undefined local variable or method `user' for
#<RSpec::Core::ExampleGroup::Nested_7::Nested_3::Nested_3::Nested_3:0x0000000640c2b0>
     # ./spec/requests/authentication_pages_spec.rb:98:in `block (5
levels) in <top (required)>'

I attach authentication_pages_spec.rb file.

Please find it.

How can I solve this error?

Kind regards.
0048ae7fd903196c7d78ed58492a736c?d=identicon&s=25 Antônio Augusto Sousa Britto (Guest)
on 2014-06-03 19:53
(Received via mailing list)
You haven't assigned anything to the "user" variable used at line 93
inside
the method call "following_user_path"

You coudl try to create an user using factory girl, inside your current
scope, perhaps using:
let(:user) { FactoryGirl.create(:user) }

Anyway, I strongly recommend that you refactor this spec in order to
make
it DRY and use context blocks in advance of so many describe blocks.





2014-06-03 14:01 GMT-03:00 Jaimin Pandya <lists@ruby-forum.com>:

>        undefined local variable or method `user' for
>
> Kind regards.
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/rubyonrails-talk...
> .
> For more options, visit https://groups.google.com/d/optout.
>



--
Att, Antônio Augusto de Sousa Britto - 43ª turma de Ciência da
Computação -
UFU
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-06-03 20:15
Antônio Augusto Sousa Britto wrote in post #1148726:
> You haven't assigned anything to the "user" variable used at line 93
> inside
> the method call "following_user_path"
>
> You coudl try to create an user using factory girl, inside your current
> scope, perhaps using:
> let(:user) { FactoryGirl.create(:user) }
>
> Anyway, I strongly recommend that you refactor this spec in order to
> make
> it DRY and use context blocks in advance of so many describe blocks.

Thank you very much for your answer.

I use following code

before { visitfollowing_user_path(FactoryGirl.create(:user)) }

in place of

before { visit following_user_path(user) } at line 93.

Now test is pass.

Is right way? can I move further in that chapter to implement
sample_app?

Kind regards.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.