2012-12-31 49 views
3

我想测试应用程序中的每一个途径,并意识到,我应该做的,在集成测试:Where to test routes in ruby on rails导轨设计验证在集成测试路线

不过,我发现了以下错误:

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' 

这是很提到网上,你不能在集成测试中使用的设计:: 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

回答

4

集成测试是您的应用程序的工作流程非常重要。他们可以更清楚地告诉你的URL定义

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

不过问题是:

制定有自己的方法,你不能在红宝石使用设计:: TestHelpers。那么你如何测试?那么你需要包括Devise :: TestHelpers莫名其妙。

好吧,如果你正在使用RSpec的,你可以把下面的一个名为spec/support/devise.rb文件中:

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

这是指定here

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

然后呢?

所以,你只需要添加设计测试助手test/test_helper.rb

class ActiveSupport::TestCase 
    include Devise::TestHelpers 
end 
6

制定:: TestHelpers工作。使用Capybara运行集成测试时,您无权访问服务器端会话。您只需访问浏览器。

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

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