簡體   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