2016-05-11 14 views
0

RSpecでRailsコントローラをテストしようとするとエラーが発生します。それは二重のネストされたルートです。私は正しい構文を理解しようとしていますが、運がまだあまりありませんでした。エラーRSpec ActionController :: UrlGenerationError with Rails

私は取得していますエラーが 失敗/エラーです:取得:インデックス、私は同じテストを行った

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)>' 

{CATEGORY_ID:category.to_param、ID system.to_param}うまく動作するシステムコントローラ。ウェブページもうまく機能します。エラーはありません(テストではこのエラーだけです)。

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 

これは、それはテストのコントローラーである:ここで

のようなRSpecのテストを見ては何です

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 
+0

あなたのルートはどこですか? – Pavan

+0

間違いをしました。あなたがルートを見ることができるように私は編集しました。 – Samuel

答えて

0

ActionController::UrlGenerationError: No route matches {: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: undefined method `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 
+0

私はそれを行うときにエラーが発生します:NoMethodError: 未定義のメソッド 'category = 'for# Samuel

+0

@Samuelあなたのモデルはどのようなものですか?質問をモデルコードで更新してください。 – Pavan

+0

私はモデルで質問を更新しました – Samuel

関連する問題