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

AJc*_*dez 3 integration-testing ruby-on-rails devise

我想测试一个应用程序中的每个路由,并了解到我应该在集成测试中做到这一点:在轨道上测试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'
Run Code Online (Sandbox Code Playgroud)

在网上很好地提到你不能在集成测试中使用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'
Run Code Online (Sandbox Code Playgroud)

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

小智 6

设计:: 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
Run Code Online (Sandbox Code Playgroud)