如何使用装置user_signed_in?集成测试中的方法?

use*_*963 2 ruby-on-rails devise

当我assert user_signed_in?进行集成测试时,它说该方法未定义。有没有办法可以在测试中使用此方法?我正在使用Rails 4和最新版本的devise。这是我的测试文件:

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
Run Code Online (Sandbox Code Playgroud)

小智 5

user_signed_in?方法包含在Devise::Controllers:Helpers模块中,该模块在集成测试中不可用,因此您无法使用它。

您可以选择模拟此方法(这并不能真正满足您的测试需求),也可以通过查找仅在用户登录时才呈现的页面内容Logout(例如,例如链接或Signed in successfully消息)来测试用户是否登录。

对于您的控制器测试,您可以使用devtest测试助手include Devise::TestHelpers,该助手为您提供sign_insign_out方法,有关更多信息,请参见Gem主页https://github.com/plataformatec/devise