错误RSpec ActionController :: UrlGenerationError与Rails

时间:2016-05-11 14:05:20

标签: ruby-on-rails ruby rspec

尝试使用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

1 个答案:

答案 0 :(得分:0)

  

ActionController :: UrlGenerationError:没有路由匹配   {:action =&gt;&#34; index&#34;,:category_id =&gt;&#34; 220&#34;,:controller =&gt;&#34;评论&#34;,   :ID =&GT;&#34; 166&#34;}

根据您的路线,该特定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 =&#39;对于   修改: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