Ruby on rails 如何在Rails集成/控制器单元测试中注销用户?
我进行了以下测试,以确保需要登录。大多数测试都需要一个已登录的用户,因此我让该用户登录安装程序。但是对于这个测试,我需要他们签出Ruby on rails 如何在Rails集成/控制器单元测试中注销用户?,ruby-on-rails,unit-testing,devise,Ruby On Rails,Unit Testing,Devise,我进行了以下测试,以确保需要登录。大多数测试都需要一个已登录的用户,因此我让该用户登录安装程序。但是对于这个测试,我需要他们签出 require 'test_helper' class DealsControllerTest < ActionDispatch::IntegrationTest include Warden::Test::Helpers setup do @deal = deals(:one) @user = users(:one) # h
require 'test_helper'
class DealsControllerTest < ActionDispatch::IntegrationTest
include Warden::Test::Helpers
setup do
@deal = deals(:one)
@user = users(:one)
# https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
@user.confirmed_at = Time.now
@user.save
login_as(@user, :scope => :user)
end
teardown do
Warden.test_reset!
end
test "require sign in for deal list" do
logout @user
get deals_url
assert_redirected_to new_user_session_path
end
需要“测试助手”
类DealsControllerTest:user)
结束
拆卸
监狱长,测试重置!
结束
测试“交易列表需要登录”do
注销@user
获取交易地址
断言\u已将\u重定向到新的\u用户\u会话\u路径
结束
我得到了错误
失败:
DealsControllerTest#test(测试)要求(登录)交易列表[C:/Users/Chloe/workspace/Fortune Empire/test/controller/deals(控制器)test.rb:35]:
预期响应为a,但为a
它说它会起作用,但就是不起作用
如果出于某种原因您需要注销已登录的测试用户,您可以使用Warden的注销助手
注销(:用户)
Rails 5.0.2自行设计4.2.1
注销
,无需任何参数。也许logout:user
也可以工作。我没有使用FactoryGirl,因此将其混淆为FactoryGirl符号。自动注销
,没有任何参数工作。也许logout:user
也可以工作。我没有使用FactoryGirl,因此将其混淆为FactoryGirl符号。正如文档中指出的,方法调用是logout(:symbol)
。您称之为传递模型。我认为你需要根据你想要注销的型号来称之为传递符号。您试过了吗?正如文档中指出的,方法调用是logout(:symbol)
。您称之为传递模型。我认为你需要根据你想要注销的型号来称之为传递符号。你试过了吗?