繁体   English   中英

rails在集成测试中设计经过身份验证的路由

[英]rails Devise authenticated routes in integration test

我想测试一个应用程序中的每个路由,并了解到我应该在集成测试中做到这一点: 在轨道上测试Ruby中的路由

但是我收到以下错误:

NoMethodError: undefined method `authenticate?' for nil:NilClass
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise-2.1.2/lib/devise/rails/routes.rb:286:in `block in authenticated'

在网上很好地提到你不能在集成测试中使用Devise :: TestHelpers - Devise Google GroupDevise Github page


如何测试以下路线?

# config/routes.rb

devise_for :users

authenticated :user do
  root to: 'static#home'
end

root to: 'static#landing'

我用$ rake test:integration运行测试单元测试

设计:: TestHelpers将工作直接放入会话中。 在使用Capybara运行集成测试时,您无权访问服务器端会话。 您只需访问浏览器即可。

在我们的应用程序中,我们的集成测试使用这样的辅助方法,通过用户界面与Devise交互:

def authenticate(user, password = nil)
  password ||= FactoryGirl.attributes_for(:user)[:password]
  visit new_user_session_path
  fill_in 'email', with: user.email
  fill_in 'password', with: password
  click_on 'Login'
  expect(current_path).to eq welcome_path
end

集成测试对您的应用程序工作流程很重要。 他们可以更清楚地了解您的网址定义

通过nicholaides检查帖子 ,解释了此错误的原因以及Authenticated路由中的解决方案。

问题仍然是:

Devise有自己的方法,你不能在ruby中使用Devise :: TestHelpers 那你怎么测试? 那你需要以某种方式包括Devise :: TestHelpers

好吧,如果你正在使用RSpec,你可以将以下内容放在名为spec/support/devise.rb的文件中:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

这是在这里指定的。

但是等等..........再次,你可以用Test::Unit遇到同样的问题。

然后?

因此,您只需要将测试助手添加到test / test_helper.rb中

class ActiveSupport::TestCase
  include Devise::TestHelpers
end

暂无
暂无

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

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