如何在Rails 5中解决“无路由匹配”功能测试错误

时间:2016-07-13 01:41:19

标签: ruby-on-rails functional-testing minitest ruby-on-rails-5

我已升级到Rails 5.获得规格通过的第一个障碍是“无路由匹配”错误。

请参阅下面的测试和test_helper。我需要添加到test_helper或test.rb吗?任何人都知道原因或如何解决这个问题?

.....

我一直在尝试简单地通过一次测试:

bin/rails test test/controllers/users_controller_test.rb:31

这是我的users_controller_test.rb中的'应该得到新'行

require 'test_helper'
describe UsersController do
//class UsersControllerTest < ActionDispatch::IntegrationTest
   before do
     glenn = users(:glenn)
     sign_in(glenn)
   end

  it 'should get new' do
    get new_user_url
    value(response).must_be :success?
  end
end

这会导致以下错误。

Error:
UsersController#test_0002_should get new:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/users/new", :controller=>"users"}
test/controllers/users_controller_test.rb:32:in `block (2 levels) in <top (required)>'

test_helper.rb中

ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/rails'

class ActionController::TestCase
  include ActiveJob::TestHelper
  include Devise::Test::ControllerHelpers
end

class ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
end

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!
  fixtures :all
  include ActionDispatch::TestProcess # fixture_file_upload
end

3 个答案:

答案 0 :(得分:0)

虽然遇到麻烦,但我创建了一个新的rails 5 app,安装了devise和minitest-rails ...而且那些测试正在通过。新的url样式语法在我的控制器测试中的describe块内工作正常。但是,在与这些问题相关的应用程序中... url语法在describe块中没有工作,并且修复---至少现在是用一个继承自ActionDispatch :: IntegrationTest的类替换describe块。我不知道,为什么会这样。

答案 1 :(得分:0)

我遇到了同样的问题并且能够让它发挥作用,尽管我并不完全确定其工作原理的内部结构。

显然,规范的类型是必要的。

我所得到的结果导致了类似的错误:

require 'rails_helper'

RSpec.describe TimingsController, type: :controller do
  describe "GET new" do
    it "renders the new template" do
      get new_timing_path
      expect(response).to be_successful
    end
  end
end

我现在的工作原理是什么:

require 'rails_helper'

RSpec.describe TimingsController, type: :controller do
  describe "GET new", type: :request do
    it "renders the new template" do
      get new_timing_path
      expect(response).to be_successful
    end
  end
end

所以似乎type: request部分是必不可少的。

答案 2 :(得分:0)

设置host对您的案例有帮助吗?

http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-host

host! "subdomain.example.com"