簡體   English   中英

帶有Rails的錯誤RSpec ActionController :: UrlGenerationError

[英]Error RSpec ActionController::UrlGenerationError with Rails

嘗試使用RSpec測試Rails控制器時出現錯誤。 這是一條雙重嵌套的路線,我正在嘗試找出正確的語法,但還沒有很多運氣。

我收到的錯誤是失敗/錯誤:get:index,{category_id:category.to_param,id:system.to_param}

ActionController::UrlGenerationError:
 No route matches {:action=>"index", :category_id=>"220", :controller=>"reviews", :id=>"166"}
 # ./spec/controllers/reviews_controller_spec.rb:11:in `block (3 levels) in <top (required)>'

我對系統控制器進行了相同的測試,效果很好。 該網頁也可以正常運行。 沒有錯誤(只有測試錯誤)。

這是RSpec測試的樣子:

require 'rails_helper'

RSpec.describe ReviewsController, type: :controller do

  let (:category) { create(:category) }
  let (:system) { create(:system) }
  let (:reviews) { create_list(:review, 3, category: category, system: system) }

 describe "GET index" do
   it "assigs all reviews to an instance var called @reviews" do
    get :index, {category_id: category.to_param, id: system.to_param}
    expect(assigns(:reviews)).to eq reviews
   end

  it "assigns all the reviews to an var called @system" do
   get :index, system_id: system.to_param
   expect(assigns(:system)).to eq system
  end
 end

 describe "system scope" do
  before { create(:review) }

  it "only assigns reviews index in the current system" do
   get :index, {category_id: category.to_param, id: system.to_param}
   expect(assigns(:reviews)).to eq reviews
  end
 end
end

這是它正在測試的控制器:

class ReviewsController < ApplicationController

  def index
   @system = System.find(params[:system_id])
   @reviews = @system.reviews

  respond_to do |format|
   format.html
   format.json { render json: { system: @system, reviews: @reviews } }
  end

 end

 def show
  @system = System.find(params[:system_id])
  @review = @system.reviews
 end

end

這些是路線:

Rails.application.routes.draw do

  root "categories#index"

  resources :categories do
    resources :systems do
      resources :reviews
    end
  end
end

這些是模型:

分類模型

class Category < ActiveRecord::Base

  validates_presence_of :name

  has_many :systems

end

系統型號

class System < ActiveRecord::Base

  belongs_to :category
  has_many :reviews

  validates_presence_of :name, :category

end

評論模型

class Review < ActiveRecord::Base

  belongs_to :system

  validates_presence_of :content, :system

end

ActionController :: UrlGenerationError:沒有路由匹配{:action =>“ index”,:category_id =>“ 220”,:controller =>“ reviews”,:id =>“ 166”}

根據您的路線,該特定index路線將category_idsystem_id作為鍵。 您需要將:id更改為:system_id 下面應該工作。

it "only assigns reviews index in the current system" do
  get :index, {category_id: category.to_param, system_id: system.to_param}
  expect(assigns(:reviews)).to eq reviews
end

更新資料

NoMethodError:未定義的方法`category ='for Review:0x005578a9e87188

reviewcategory之間沒有關聯。 編輯模型以相應地設置關聯。

#category.rb
class Category < ActiveRecord::Base
  validates_presence_of :name
  has_many :systems
  has_many :reviews
end

#review.rb
class Review < ActiveRecord::Base
  belongs_to :system
  belongs_to :category
  validates_presence_of :content, :system
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM