我正在尝试进行基本的rspec测试,看起来像这样;
require 'rails_helper'
describe ReviewsController do
describe "GET #index" do
it "assigns a new review to @reviews" do
review = Review.create( rating: 4 )
get :index
expect(assigns(:review)).to eq([review])
assert_response :success
end
end
end
但我得失败:预期:复习ID:8,评分:4,created_at:“2016-07-19 11:58:28”,updated_at:“2016-07-19 11:58:28 “,user_id:nil,game_id:nil 得到了:没有了
我的ReviewsController看起来像这样:
class ReviewsController < ApplicationController
def index
@reviews = Review.all
end
def show
@review = Review.find(params[:rating])
end
def create
review = Review.new(review_params)
respond_to do |format|
if @review.save
format.html { redirect_to root_url, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :new }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
private
def post_params
params.require(:post).permit(:message)
end
end
如果您需要,请参阅以下评论模型:
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :game
validates_presence_of :rating
validates_uniqueness_of :user_id
end
我不明白为什么要求user_id或game_id,因为它只是关于评论..
答案 0 :(得分:0)
您必须更改以下
expect(assigns(:review)).to eq([review])
到
expect(assigns(:reviews)).to eq([review])
原因是@reviews
是#index
控制器操作中的实例变量,而不是@review
。