简体   繁体   English

与Devise和rspec的Rails 3:Warden测试助手不可靠

[英]Rails 3 with Devise and rspec: Warden test helpers unreliable

I have an application using rails 3.2 and Devise. 我有一个使用rails 3.2和Devise的应用程序。 I have a request test suite using rspec and Capybara. 我有一个使用rspec和Capybara的请求测试套件。

I have attempted to shift to using the Warden test helpers in my sign in helper rather than having Capybara fill in the sign in form and submit it. 我试图在我的登录帮助中转而使用Warden测试助手,而不是让Capybara填写登录表单并提交。 Due to the size and complexity of my test suite, this results in over a minute and a half of savings in my test runtimes. 由于我的测试套件的大小和复杂性,这导致我的测试运行时节省超过一分半。

In my configuration is: 在我的配置是:

RSpec.configure do |config|
  config.include Warden::Test::Helpers, :type => :request
  config.after :each do
    Warden.test_reset!
  end
end

And in a Context: 并在上下文中:

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

background do
  login_as(current_user, :scope => :user)
end

However, when running the test suite using these configurations nearly every test run has a different randomly failing request spec due to the page presenting as though the user were not logged in. (Specifically, links controlled by a user_signed_in? conditional) 但是,当使用这些配置运行测试套件时,几乎每个测试运行都会有一个不同的随机失败的请求规范,因为页面呈现就好像用户没有登录一样。(具体来说,由user_signed_in?控制的链接user_signed_in?条件)

My question: Has anyone else encountered such unreliability and how can it be mitigated? 我的问题:是否有其他人遇到过这种不可靠性以及如何减轻这种不可靠性?

The absolute easiest thing to do is just use the sign_in form in your before :each block. 绝对最简单的方法就是使用之前的sign_in表单before :each块。 You need the test helpers in Controller specs, because it's isolated from devise. 您需要Controller规范中的测试助手,因为它与设计隔离。 You don't have that problem in an integration test. 您在集成测试中没有这个问题。

Though an old post I also have this problem with Rails 4.1 and Warden 1.2.3. 虽然一个旧帖子我也有Rails 4.1和Warden 1.2.3的这个问题。 I noticed that there is a newer version of Warden with work on the test helpers. 我注意到有一个较新版本的Warden在测试助手上工作。 Anyone success with the newer version? 有没有新版本的成功?

Btw the behavior that I observe is that Warden does not always successfully login via the login_as helper. 顺便说一下,我观察到的行为是Warden并不总是通过login_as助手成功登录。 This results in my app finding itself on the login page instead of the place it thought it ought to be. 这导致我的应用程序在登录页面上找到自己而不是它认为应该的位置。

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

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