Rails Controller Spec无法找到路由

时间:2017-12-30 23:24:10

标签: ruby-on-rails rspec controller

当我从浏览器访问我的页面(控制器操作)时,页面渲染正常。 当我尝试从控制器规范访问该页面时,它失败了。

使用浏览器记录页面的消息:

Started GET "/listings/2" for ::1 at 2017-12-30 17:10:13 -0500
Processing by Web::ListingsController#show as HTML
  Parameters: {"id"=>"2"}
  Listing Load (0.3ms)  SELECT  "listings".* FROM "listings" WHERE "listings"."id" = $1 LIMIT 1  [["id", 2]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Rendered web/listings/show.html.erb within layouts/web/application (0.8ms)
Completed 200 OK in 12ms (Views: 3.5ms | ActiveRecord: 0.5ms)
source=rack-timeout id=b2c36cc7e27a8a1be766ef1d6f5d571a timeout=10000ms service=16ms state=completed

我的错误来自规范:

  1) Web::ListingsController when logged in GET #show assigns the requested listing as @listing
     Failure/Error: get :show, params: {id: listing.to_param}

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"web/listings", :params=>{:id=>"158"}}

我很确定我在某个地方错过了某种配置设置,但经过几个小时的挖掘后,我没有看到它。有人能指出我可以解决这个问题吗?

相关路线条目:

Rails.application.routes.draw do
  resources :listings, controller: "web/listings"
end

规格/控制器/网络/ listings_controller_spec.rb

RSpec.describe Web::ListingsController, type: :controller do
  describe "GET #show" do
    it "assigns the requested listing as @listing" do
      listing = FactoryBot.create(:listing, user: @user)
      @puts controller.url_for(listing)
      get :show, params: {id: listing.to_param}
      expect(assigns(:listing)).to eq(listing)
    end
  end
end

and the output of that puts statement: http://test.host/listings/158

应用程序/控制器/网络/ listings_controller.rb

class Web::ListingsController < ApplicationController
   def show 
      ...
   end

rake路线的输出| grep列表

       listings GET    /listings(.:format)            web/listings#index
                 POST   /listings(.:format)            web/listings#create
     new_listing GET    /listings/new(.:format)        web/listings#new
    edit_listing GET    /listings/:id/edit(.:format)   web/listings#edit
         listing GET    /listings/:id(.:format)        web/listings#show
                 PATCH  /listings/:id(.:format)        web/listings#update
                 PUT    /listings/:id(.:format)        web/listings#update
                 DELETE /listings/:id(.:format)        web/listings#destroy
       user_root GET    /user_root(.:format)           web/listings#index

模型不在web子目录中:app / models / listing.rb

rails 4.2.10 rspec 3.5.0

2 个答案:

答案 0 :(得分:2)

嵌套路由和嵌套控制器不是一回事。假设你确实想要一个像网页/列表&#39;尝试:

Rails.application.routes.draw do
  namespace :web do
    resources :listings
  end
end

如果您不想/需要命名路由,请尝试命名帮助程序,例如。 get listing_path(listing)

答案 1 :(得分:0)

问题在于规范,特别是如何传递参数。

RSpec.describe Web::ListingsController, type: :controller do
  describe "GET #show" do
    it "assigns the requested listing as @listing" do
      listing = FactoryBot.create(:listing, user: @user)
      get :show, {id: listing.to_param}
      expect(assigns(:listing)).to eq(listing)
    end
  end
end 

不要包含params:位。此代码来自脚手架生成器:/