运行RSpec Test时的UrlGenerationError

时间:2015-07-21 15:36:47

标签: ruby-on-rails testing rspec controller shoulda

我正在尝试运行测试,以确保为餐馆呈现我的show模板。运行测试后,我得到:

 1) RestaurantsController GET #show 
     Failure/Error: before { get :show }
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"restaurants"}

当确实有显示餐馆的路线时,不确定为什么会说:

    restaurants GET    /restaurants(.:format)          restaurants#index
                POST   /restaurants(.:format)          restaurants#create
 new_restaurant GET    /restaurants/new(.:format)      restaurants#new
edit_restaurant GET    /restaurants/:id/edit(.:format) restaurants#edit
     restaurant GET    /restaurants/:id(.:format)      restaurants#show
                PATCH  /restaurants/:id(.:format)      restaurants#update
                PUT    /restaurants/:id(.:format)      restaurants#update
                DELETE /restaurants/:id(.:format)      restaurants#destroy

测试

require "rails_helper"

describe RestaurantsController do
  describe "GET #show" do
    before { get :show }

    it { should render_template("show") }
  end
end

1 个答案:

答案 0 :(得分:0)

您忘记了id

before { get :show, id: 1 } # Assuming there is a Restaurant with id=1