繁体   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