路由断言不识别路由

时间:2017-10-02 22:28:47

标签: ruby-on-rails routes

我正在尝试在我的rails引擎中设置一个映射到我的控制器操作的自定义路由,但是在测试路由时它表示没有路由匹配它。

的routes.rb

MyEngine::Engine.routes.draw do
    resources :lead_emails, except: [:new, :edit, :show]
    get 'lead_emails/send_out_emails/:idea_id', to: 'lead_emails#send_out_emails', as: 'send_out_emails'
end

佣金路线

Routes for MyEngine::Engine:
    lead_emails GET    /lead_emails(.:format)                          my_engine/lead_emails#index
                POST   /lead_emails(.:format)                          my_engine/lead_emails#create
     lead_email PATCH  /lead_emails/:id(.:format)                      my_engine/lead_emails#update
                PUT    /lead_emails/:id(.:format)                      my_engine/lead_emails#update
                DELETE /lead_emails/:id(.:format)                      my_engine/lead_emails#destroy
send_out_emails GET    /lead_emails/send_out_emails/:idea_id(.:format) my_engine/lead_emails#send_out_emails

我尝试过多种方式测试这条路线

test "should generate send out emails route" do
      assert_generates( 'lead_emails/send_out_emails/1',
      {controller: 'my_engine/lead_emails', action: 'send_out_emails', idea_id: "1"})
end

ActionController::UrlGenerationError: 
No route matches {:action=>"send_out_emails", :controller=>"my_engine/lead_emails", :idea_id=>"1"}


test "should recognize send out emails route should exist" do
    assert_recognizes(
          {controller: 'my_engine/lead_emails', action: 'send_out_emails', idea_id: "1"},
          'lead_emails/send_out_emails/1')
end

No route matches "/lead_emails/send_out_emails/1"

疯狂的是,如果我使用辅助方法,它可以工作

test "should send out emails" do   
    get send_out_emails_path(@idea.id)
    assert_response :success 
end  

此测试通过。那么我该如何测试这条路线呢?看起来很漂亮,但它一直让我发疯!我已尝试使用和没有控制器的my_engine前缀,但没有帮助任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

我真的不知道为什么会有效,但它确实有效。我在我的测试

中为我的setup方法添加了一个变量
setup do
    @routes = MyEngine::Engine.routes
end 

现在每个测试都有效。