本文介绍了在集成测试中设计经过验证的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试应用程序中的每条路由,并了解到应该在集成测试中做到这一点:

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

但是我遇到以下错误:

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-

It's well-mentioned online that you can't use Devise::TestHelpers in integration testing --Devise Google Group, Devise Github page

>如何测试以下路线?

# config/routes.rb

devise_for :users

authenticated :user do
  root to: 'static#home'
end

root to: 'static#landing'

我正在使用 $ rake test:integration

推荐答案

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

Integration tests are important for your application work flow. They can tell about your URL definition more clearly.

检查由 nicholaides ,它说明了此错误的原因以及经过身份验证的路由中的解决方案。

Check the post by nicholaides, that explains the cause of this error and the solution in Authenticated routes.

问题仍然存在:

Devise有其自己的方法,您不能在ruby中使用 Devise :: TestHelpers 。那么如何测试?好吧,您需要以某种方式包含 Devise :: TestHelpers

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.

好吧,如果您使用的是RSpec,则可以将以下内容放到名为 spec / support / devise.rb 的文件:

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

这是。

但是请稍候.......... 同样,您可以使用 Test :: Unit 遇到相同的问题。

But wait.......... Again, you can run into this same issue with Test::Unit.

然后呢?

因此,您只需要将设计测试助手添加到 test / test_helper.rb

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

class ActiveSupport::TestCase
  include Devise::TestHelpers
end

这篇关于在集成测试中设计经过验证的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:14