[英]rails Devise authenticated routes in integration test
I want to test every route in an application, and learned I should do that in an integration test: Where to test routes in ruby on rails 我想测试一个应用程序中的每个路由,并了解到我应该在集成测试中做到这一点: 在轨道上测试Ruby中的路由
However I'm getting the following error: 但是我收到以下错误:
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'
It's well-mentioned online that you can't use Devise::TestHelpers in integration testing -- Devise Google Group , Devise Github page 在网上很好地提到你不能在集成测试中使用Devise :: TestHelpers - Devise Google Group , Devise Github page
How can I test routes like the following? 如何测试以下路线?
# config/routes.rb
devise_for :users
authenticated :user do
root to: 'static#home'
end
root to: 'static#landing'
I am running test unit tests with $ rake test:integration
我用
$ rake test:integration
运行测试单元测试
Devise::TestHelpers work by putting things directly into your session. 设计:: TestHelpers将工作直接放入会话中。 When running integration tests with Capybara, you don't have access to the server-side session.
在使用Capybara运行集成测试时,您无权访问服务器端会话。 You just have access to the browser.
您只需访问浏览器即可。
In our application, our integration tests use helper methods like this, that interact with Devise through the user interface: 在我们的应用程序中,我们的集成测试使用这样的辅助方法,通过用户界面与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
Integration tests are important for your application work flow. 集成测试对您的应用程序工作流程很重要。 They can tell about your URL definition more clearly.
他们可以更清楚地了解您的网址定义 。
Check the post by nicholaides
, that explains the cause of this error and the solution in Authenticated routes. 通过
nicholaides
检查帖子 ,解释了此错误的原因以及Authenticated路由中的解决方案。
Still the problem is: 问题仍然是:
Devise has its own methods and you can't use Devise::TestHelpers in ruby. Devise有自己的方法,你不能在ruby中使用Devise :: TestHelpers 。 So how can you test?
那你怎么测试? Well you need to include the Devise::TestHelpers somehow.
那你需要以某种方式包括Devise :: TestHelpers 。
Well, if you're using RSpec, you can put the following inside a file named spec/support/devise.rb
: 好吧,如果你正在使用RSpec,你可以将以下内容放在名为
spec/support/devise.rb
的文件中:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
This is specified here . 这是在这里指定的。
But wait.......... Again, you can run into this same issue with Test::Unit
. 但是等等..........再次,你可以用
Test::Unit
遇到同样的问题。
Then? 然后?
So, you just need to add the devise test helpers to test/test_helper.rb . 因此,您只需要将测试助手添加到test / test_helper.rb中 。
class ActiveSupport::TestCase
include Devise::TestHelpers
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.