I'm trying to go through Michael Hartl's RoR tutorial, but it's my third time through the thing and now I'm trying to implement Devise instead of the home rolled authentication in the tutorial. As you can imagine, this is a little bit painful, mainly due to the test driven development in the RoR tutorial not mixing well with Devise. My current hang up is that I can't seem to successfully test a successful login. I have the website up on localhost, so I know the login works, but my test is still failing for some reason. Here's the integration test snippet
@user = users(:michael)
test "login with valid information" do
assert_equal 200, status
post user_session_path, 'user[email]' => @user.email, 'user[password]' => "password"
assert_equal 200, status
assert_select "a[href=?]", user_session_path, count: 0 # THIS LINE FAILS
assert_select "a[href=?]", destroy_user_session_path
assert_select "a[href=?]", user_path(@user)
The error I get is
FAIL["test_login_with_valid_information", UsersLoginTest, 2016-06-19 11:06:18 -0400]
Expected exactly 0 elements matching "a[href="/users/sign_in"]", found 1..
The login bar up at the top switches from `Sign in` to `Account`, so it looks like the test isn't getting the user in the setup block successfully signed in. Another, possibly unrelated, issue is that in another test I have, I try the same exact login method with `post user_session_path...` and then check that `current_user` is not null with `assert_not current_user.nil?`, but I get the error
NameError: undefined local variable or method 'current_user' for #
I've looked around and made sure that I have the correct lines of `devise_for :users` in my routes.rb file. I'm pretty sure It's not able to access `current_user` because somehow I don't have my integration test able to access resources in Devise. Those are my two problems! Any help would be greatly appreciated.
3 thoughts on “Testing successful login with devise”
Haven’t worked on this stuff for a while but I distinctly remember devise not working at all on windows and I had to move to C9.io to develop in a Linux environment.
Highly recommend testing it on that
I use capybara and it actually fills in and submits the form. And I can run that in a browser so I can visually see what’s going on. It’s slower that way however, and I couldn’t get it working for remote tests so I switched to poltergeist (was using selenium) and now it does headless browser testing.
Mine looks much different than yours but I’m using rspec and I think you’re using minitest.
Hey there, yeah it is possible that your user isn’t being signed in for some reason. For debugging a situation like this I typically use the [pry gem] (https://github.com/pry/pry).
With this, I can call `binding.pry` anywhere in my code, or even in 3rd party gem code and I’ll get console access to the local scope exactly at that point.
Does the user actually exist in the test database when the test is run? You could do `binding.pry` before your post to the `user_session_path` and then check.