繁体   English   中英

如何使用设计 user_signed_in? 集成测试中的方法?

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

当我assert user_signed_in? 在集成测试中,它说该方法未定义。 有没有办法在我的测试中使用这种方法? 我正在使用 rails 4 和最新版本的设计。 这是我的测试文件:

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

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

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

对于您的控制器测试,您可以使用设计测试助手include Devise::TestHelpers ,它为您公开sign_insign_out方法,更多关于 Gem 的主页https://github.com/plataformatec/devise

你不能使用user_signed_in? 在我之前提到的集成测试中,但您可以编写一个简单的辅助方法来帮助您模仿这种行为

我所做的是在 test_helper.rb 中:

 def is_logged_in?
  request.env['warden'].authenticated?(:user)
 end

这是一个非常hacky的解决方案,但它确实有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM