2017-07-25 92 views
1

在这里搜索了其他类似的问题,它们似乎都与某种格式错误的请求有关(不提供id,或者不包括params正确),但那些不似乎是我遇到的问题。Rails 5 Rspec ActionController :: UrlGenerationError

这条路线绝对存在,去浏览器中的路由工作得很好。页面加载,一切。但是由于某种原因,Rspec给了我一个UrlGenerationError。

我试过在路由中手动指定控制器名称,更改为复数控制器,甚至使用不同的(复数)控制器。这看起来好像某处有其他配置存在问题,但是它是一个URLGeneration错误的错误是非常无益的。我会非常感激任何其他想法。

我们在我们的应用程序中的其他地方有API控制器规范,似乎运行正常,但我无法分辨这些与这个之间的设置差异。

我的错误:

1) Spaceman::ReputationEnhancementsController GET show has a 200 status code 
    Failure/Error: get :show 

    ActionController::UrlGenerationError: 
     No route matches {:action=>"show", :controller=>"spaceman/reputation_enhancements"} 
    # ./spec/controllers/spaceman/reputation_enhancements_controller_spec.rb:10:in `block (3 levels) in <top (required)>' 

Finished in 1.14 seconds (files took 6.13 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/controllers/spaceman/reputation_enhancements_controller_spec.rb:9 # Spaceman::ReputationEnhancementsController GET show has a 200 status code 

路线:

Spaceman::Engine.routes.draw do 
    resource :reputation_enhancement, path: "reputation-enhancement", only: [ :show, :create ] do 
    get :filter 
    end 
end 

测试:(我试过加type: :controller这里,以及完全限定,加入Rspec.describe代替,等...)

require "rails_helper" 

describe Spaceman::ReputationEnhancementsController do 

    describe "GET show" do 
    it "has a 200 status code" do 
     get :show 
     expect(response.status).to eq(200) 
    end 
    end 

end 

rake routes

filter_reputation_enhancement GET /reputation-enhancement/filter(.:format) spaceman/reputation_enhancements#filter 
     reputation_enhancement GET /reputation-enhancement(.:format)  spaceman/reputation_enhancements#show 
           POST /reputation-enhancement(.:format)  spaceman/reputation_enhancements#create 

编辑:

我已经尝试在路由手动指定控制器名称,改变为多元化的控制器,并且甚至使用不同的(多元状态)控制器。这看起来好像某处有其他配置存在问题,但是它是一个URLGeneration错误的错误是非常无益的。我会非常感激任何其他想法。

+0

这实际上是一个重复的:https://stackoverflow.com/questions/40978962/testing-singular-resource-controller-with-rspec – coreyward

回答

1

原来,问题在于控制器是宝石的一部分,访问控制器的路由默认不包含在内。

通过将routes { Spaceman::Engine.routes }添加到主describe块的顶部内来解决。

离开单数路线并且所有其他设置都正确设置都很好。只需要包含路线。

+0

感谢,'routes {Rails.application.routes}'工作。有没有一个方便的方法来做到这一点?把所有的'describe'块加入这个很麻烦。 – HFX

+0

嗯。我没有更深入,我很抱歉。我确实有一种方法可以在rspec中配置某种选项来自动设置。 – Rockster160

0

您在路线中使用单数resource,但您的规格要求复数形式“reputation_enhancement s”。

请参阅this bit in the routing guide for singular resourcesthis long standing issue在Rails问题跟踪器中解释说,没有好方法可以让url_for映射回单一资源。

要解决它,请在规范中指定路径或添加复数路线。

+0

请求复数形式的规范在哪里?控制器应该是多元化的,即使是单独的路线,如果这就是你所指的 - 所以我不确定复数是在哪里搞砸了? – Rockster160

+0

将所有对控制器的引用切换为单数(通过在路由中手动分配控制器)并且问题仍然存在。 – Rockster160

+0

@ Rockster160我为你增加了更多信息。 – coreyward

相关问题