在集成测试中设计认证路线(rails Devise authenticated routes in integration test)

编程入门 行业动态 更新时间:2024-10-27 22:33:59
在集成测试中设计认证路线(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 - 设计Google Group , Devise Github页面


我如何测试下列路线?

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

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

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

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


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

最满意答案

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

通过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

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.

Still the problem is:

Devise has its own methods and you can't use Devise::TestHelpers in ruby. So how can you test? Well you need to include the Devise::TestHelpers somehow.

Well, if you're using RSpec, you can put the following inside a file named 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.

Then?

So, you just need to add the devise test helpers to test/test_helper.rb.

class ActiveSupport::TestCase include Devise::TestHelpers end

更多推荐

本文发布于:2023-08-01 17:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路线   测试中   rails   Devise   test

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!