简体   繁体   English

索引rspec测试失败

[英]index rspec test failing

Im having difficulties with writing a spec for an index action of a controller im trying to test. 我在编写要测试的控制器的索引操作规范时遇到困难。 The controller looks like this: 控制器如下所示:

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 

and my spec looks like this: 和我的规格看起来像这样:

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 

The error that the spec returns says this: 规范返回的错误说明:

Failure/Error: get :index ActiveRecord::RecordNotFound: Couldn't find Contest with 'id'= 失败/错误:get:index ActiveRecord :: RecordNotFound:找不到带有“ id”的竞赛=

Can anyone figure out what is wrong? 任何人都可以找出问题所在吗?

Solved it! 解决了! had to do this: 必须这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM