Ruby 在 rails 上:Rspec 测试成功的 http 状态时出现路由错误

Ruby on rails: Rspec routing error when testing for a successful http status

所以我无法在 rails 应用程序的 ruby 中传递这些规范:

require 'rails/all'

RSpec.describe WikisController, type: :controller do 
let(:user) { User.create!(email: "user@email.com", password: "password") }
let(:wiki) { Wiki.create!(title: "New Wiki Title", body: "New Wiki Body", private: false, user: user) }


   describe "GET show" do
     it "returns http success" do
       get :show
       expect(response).to have_http_status(:success)
     end
   end

   describe "GET edit" do
     it "returns http success" do
       get :edit
       expect(response).to have_http_status(:success)
     end
   end
end

当我 运行 这些规格时,我得到这些错误:

Failures:

  1) WikisController GET show returns http success
     Failure/Error: get :show

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

  2) WikisController GET edit returns http success
     Failure/Error: get :edit

     ActionController::UrlGenerationError:
       No route matches {:action=>"edit", :controller=>"wikis"}
     # ./spec/controllers/wikis_controller_spec.rb:23:in `block (3 levels) in <top (required)>'

我感到困惑的原因是它们在浏览器中工作正常。另外,当我 运行 耙路线时,我得到这个:

                    root GET    /                              wikis#index
                   wikis GET    /wikis(.:format)               wikis#index
                         POST   /wikis(.:format)               wikis#create
                new_wiki GET    /wikis/new(.:format)           wikis#new
               edit_wiki GET    /wikis/:id/edit(.:format)      wikis#edit
                    wiki GET    /wikis/:id(.:format)           wikis#show
                         PATCH  /wikis/:id(.:format)           wikis#update
                         PUT    /wikis/:id(.:format)           wikis#update
                         DELETE /wikis/:id(.:format)           wikis#destroy

所以,这些错误对我来说没有意义,因为显然我确实有那些控制器的路线。有人可以告诉我这里发生了什么吗?

您的 rspec 包括 show, edit 项操作,这些操作需要 id 作为您来自 rake routes 的结果。

尝试get :show, id: 1