0

I've searched around and this seems like a common enough issue, but nothing has worked for me. Here goes!

Firstly, I'm working through the Hartl RoR tutorial. I'm towards the end of section 9 (video 905) and I'm trying to test a failing user login. Here's the code in user_spec.rb:

   describe "failure" do  
     it "should not log the user in" do  
       visit login_path  
       puts response.body  
       fill_in 'session_email',    with: ""  
       fill_in 'session_password', with: ""   
       click_button  
       response.should have_selector('div.login_error', content: "Invalid")  
       response.should render_template('sessions/new')  
     end  
   end

and here's the autotest error:

  1) User login failure should not log the user in
 Failure/Error: visit login_path
 NameError:
   undefined local variable or method `login_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c79248>
 # ./spec/models/user_spec.rb:176:in `block (4 levels) in <top (required)>'

I tried writing visit login_path as visit '/login', and still got an error, although it changed to this:

Failure/Error: visit '/login'
 NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c2d140>

Which makes me think this is maybe a webrat issue, as I read somewhere that 'visit' comes from webrat (and not rspec). I also know that it's not getting past the first line, as the puts response.body isn't showing up on the autotest failure. More confusing is that in another part of my app, I have other test code that's virtually identical and it works fine.

layout_links_spec.rb:

before(:each) do  
  @user = Factory(:user)  
  visit login_path  
  # puts response.body  
  fill_in 'session_email',    with: @user.email  
  fill_in 'session_password', with: @user.password  
  click_button  
end

Any ideas?

dax
  • 10,779
  • 8
  • 51
  • 86
  • Did you miss any include statements in the user_spec.rb. Compare the includes in layout_links_spec.rb with user_spec.rb – manoj Apr 07 '13 at 08:57
  • both have `require 'spec_helper'` – dax Apr 07 '13 at 09:32
  • I think something is amiss here. Have a look at [the Rails Tutorial **user_spec.rb**](https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/models/user_spec.rb) that tests the `User` model; no `*_path` tests. Capybara tests that `visit` a `page` would be in the [**requests/user_pages_spec.rb**](https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/requests/user_pages_spec.rb). – Paul Fioravanti Apr 07 '13 at 10:49
  • hmm...i don't have a `user_pages_spec.rb` - I'm not using Capybara, either, maybe I'm using a different version? that [Rails Tutorial user_spec.rb](https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/models/user_spec.rb) has different syntax than what I have - here's [my user_spec.rb](http://textuploader.com/?p=6&id=OL87U). – dax Apr 07 '13 at 10:59
  • ah, sorry, i meant to say 'maybe i'm using a different version of the Hartl tutorial' – dax Apr 07 '13 at 12:06
  • You're not using the one (there are others?) at [http://ruby.railstutorial.org](http://ruby.railstutorial.org)? – Paul Fioravanti Apr 07 '13 at 12:36

1 Answers1

1

I had to do a lot of updates to get your code running. I haven't done Rails on my home machine for a while so it was good to get it all up to date.

Found a solution to getting named routes to work but that just led to another problem. Finally the light went on. You are doing the test in the wrong place. The user model spec is not where you test login. As you mentioned, another similar test works. That is in the Request specs. The visit command and named routes are designed to work in that directory.

EricM
  • 754
  • 1
  • 5
  • 16
  • [here](http://textuploader.com/?p=6&id=NnvR)'s my rake routes. I changed it to 'login' just to keep it more separate in my mind from 'signup'. – dax Apr 07 '13 at 20:33
  • Thanks so much for your help, that was it! Feel a bit dumb that I didn't try moving things around before. – dax Apr 10 '13 at 13:16