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?
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.