When I have assert user_signed_in?
in an integration test it says the method is undefined. Is there a way I can use this method in my testing? I am using rails 4 and the latest version of devise. Here is my test file:
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
test "valid signup information" do
get new_user_registration_path
assert_difference 'User.count', 1 do
post_via_redirect user_registration_path,
user: { first_name: "Example",
last_name: "User",
email: "user@example.org",
password: "password",
password_confirmation: "password" }
end
assert_template 'activities/index'
assert user_signed_in?
end
The user_signed_in?
method is included in Devise::Controllers::Helpers
module which isn't available in your integration tests so you can't use it.
You have the option of either mocking this method (which won't really meet your testing needs) or testing that a user is signed in by looking for page content that will only render when the user is signed in like Logout
link for example or Signed in successfully
message.
For your controller tests you can use devise test helpers include Devise::TestHelpers
which exposes a sign_in
and sign_out
methods for you, more on that in the Gem's home page https://github.com/plataformatec/devise
you can't use user_signed_in?
inside integration tests as mentioned here before me, but you can write a simple helper method to help you mimic this behavior
what i did is ,inside the test_helper.rb :
def is_logged_in?
request.env['warden'].authenticated?(:user)
end
it's a pretty hacky solution but it does the trick
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.