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

我想测试一个应用程序中的每个路由,并了解到我应该在集成测试中做到这一点: 在轨道上的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 Group , Devise Github page


如何测试以下路线?

 # config/routes.rb devise_for :users authenticated :user do root to: 'static#home' end root to: 'static#landing' 

我正在使用$ rake test:integration测试unit testing

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

通过nicholaides检查post ,解释了此错误的原因以及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 

设计:: 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