Capybara,RSpec和Devise:通过避免缓慢登录和直接设置会话,任何方式可以更快地进行集成测试? [英] Capybara, RSpec and Devise: any way to make integration tests faster by circumventing slow login and setting session directly?

查看:125
本文介绍了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 :: Helpers Warden.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 然后你可以运行任何测试,你可以确定用户已经加入了。我想这是你想要的,希望这个帮助,你可以在这里看到更多的细节用水瓶座测试

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.

EDIT

为了确保这样工作正常,你需要重新设置每个测试后的监护人,你可以通过调用

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天全站免登陆