我正在尝试编写控制器的索引操作的规范时遇到困难。控制器如下所示:
class MyGamesResultsController < ApplicationController
def index
@contest = Contest.find(params[:contest_id])
@my_entry = current_user.entries.where(contest_id: params[:contest_id])
@points_per_player = @my_entry[0].points_per_player
@total_points = @my_entry[0].total_points
end
end
我的规格如下:
require 'rails_helper'
RSpec.describe MyGamesResultsController, type: :controller do
describe 'GET /index' do
let!(:user) { create(:user) }
before :each do
sign_in user
get :index
end
it 'renders the index template' do
expect(subject).to render_template(:index)
end
end
end
规范返回的错误说明:
失败/错误:获取:索引 ActiveRecord的:: RecordNotFound: 找不到'id'=
的比赛任何人都可以找出问题所在吗?
答案 0 :(得分:0)
解决了!必须这样做:
require 'rails_helper'
RSpec.describe MyGamesResultsController, type: :controller do
describe "GET index" do
let!(:user) { create(:user) }
let!(:contest) { create(:contest) }
let!(:my_entry) { create(:entry, user_id: user.id, contest_id: contest.id) }
before :each do
sign_in user
get :index, contest_id: contest.id
end
it "renders the index template" do
(expect (response.status)).to eql(200)
end
end
end