Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the devise user_signed_in? method in an integration test?

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:      "[email protected]",
                                             password:              "password",
                                             password_confirmation: "password" }
    end
    assert_template 'activities/index'
    assert user_signed_in?
  end
like image 743
user4584963 Avatar asked Dec 19 '25 03:12

user4584963


2 Answers

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

like image 62
bigsolom Avatar answered Dec 20 '25 19:12

bigsolom


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

like image 36
Moshe Edri Avatar answered Dec 20 '25 18:12

Moshe Edri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!