Capybara、RSpec 和 Devise:有什么方法可以通过绕过缓慢的登录和直接设置会话来加快集成测试? [英] Capybara, RSpec and Devise: any way to make integration tests faster by circumventing slow login and setting session directly?

查看:16
本文介绍了Capybara、RSpec 和 Devise:有什么方法可以通过绕过缓慢的登录和直接设置会话来加快集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于几乎每个集成测试,用户都必须登录 Devise.这需要很多时间,所以我想知道是否有一种方法可以设置用户会话,而无需访问登录页面,输入详细信息,然后按登录按钮.

For nearly every integration test, a user has to be signed into Devise. This takes a lot of time, so I wondered whether there's a way to set up the user session without having to visit the login page, enter details, and press the login button.

也许 Devise 中内置了一个帮助方法,可以立即让给定用户登录?

Maybe there's a helper method built into Devise that immediately signs a given user in?

非常感谢您的帮助.

推荐答案

在规范文件的标题中,插入 include Warden::Test::HelpersWarden.test_mode!,像这样:

In the header of your spec file, insert include Warden::Test::Helpers and Warden.test_mode!, like this:

require 'spec_helper'

include Warden::Test::Helpers
Warden.test_mode!

describe "AuthenticationPages" do

let(:user) { FactoryGirl.create(:user) }

before { login_as(user, scope: :user }
...

在上面的代码中,我使用 FactoryGirl 创建一个用户.您可以使用您喜欢的其他方式创建用户.然后我使用方法 login_as 登录 user .然后你可以运行任何测试,你可以确定 user 已经登录.我想这就是你想要的,希望对你有所帮助.您可以在此处使用水豚测试查看更多详细信息.

In above code, i used FactoryGirl to create an user. You can use other ways you like to create user. Then I login user by using method login_as . Then you can run any test and you can sure user has loged in. I think this is what you want, hope this help. You can see more details here Test with capybara.

编辑

为了确保它正常工作,您需要在每次测试后重置warden,您可以通过调用来执行此操作

To make sure this works correctly you will need to reset warden after each test you can do this by calling

Warden.test_reset! 

如果由于某种原因您需要注销已登录的测试用户,您可以使用 Warden 的注销助手.

If for some reason you need to log out a logged in test user, you can use Warden's logout helper.

logout(:user)

这篇关于Capybara、RSpec 和 Devise:有什么方法可以通过绕过缓慢的登录和直接设置会话来加快集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆