在 Rspec 中不断出现未定义的方法错误

Keep getting undefined method error in Rspec

我有一个控制器操作 'show' 我正在尝试测试。控制器看起来像这样:

def show
  @contest = Contest.find(params[:contest_id])
  @my_entry = current_user.entries.where(contest_id: params[:contest_id]).sort_by { |entry| -entry.total_points}
  @points_per_player = @my_entry[0].points_per_player
  @total_points = @my_entry[0].total_points
  @opponents = @contest.entries.where.not(user_id: current_user.id).sort_by { |entry| -entry.total_points}
  @opponent_squad = Squad.find(@opponents[0].squad_id)
  @opponent_points = @opponents[0].points_per_player
end 

show 动作的规范如下所示:

describe 'GET /show' do
   let!(:user) { create(:user) }
   let!(:squad_1) { create(:squad) }
   let!(:squad_2) { create(:squad) }
   let!(:contest) { create(:contest) }
   let!(:my_entry) { create(:entry, user_id: user.id, contest_id: contest.id) } 
   let!(:opponents) { [:squad_1, :squad_2] } 
   let!(:opponent_squad) { create(:squad) }

 before :each do
   sign_in user
   controller.instance_variable_set(:@opponent_squad, :squad )
   get :show, contest_id: contest.id
 end

 it "renders the show template" do
   (expect (response.status)).to eql(200)
   (expect (assigns(:opponents))).to eql([:squad_1, :squad_2])
   (expect (assigns(:opponent_squad))).to eql(:squad)
 end
end

Rspec 不断抛出错误 "undefined method `squad_id' for nil:NilClass",我认为这是指 show 动作中的倒数第二行,即:

@opponent_squad = Squad.find(@opponents[0].squad_id)

知道为什么会发生这种情况以及如何解决吗? (请注意,我对 Rspec 非常陌生)

我认为你的问题是这一行

 @opponents = @contest.entries.where.not(user_id: current_user.id).sort_by { |entry| -entry.total_points}

您正在查找 user_id 不是当前用户的所有条目。但是在您的规范中,您创建的条目

  let!(:my_entry) { create(:entry, user_id: user.id, contest_id: contest.id) }

将 user_id 设置为当前用户,以便@opponents 集合不会以任何结果结束。

然后你寻找@opponents 有一个值

 @opponent_squad = Squad.find(@opponents[0].squad_id)

并获得异常。

我有两点建议。首先更新规范以确保已使用不同的 user_id 创建了该条目 class 的实例,以便@opponents 获得结果,然后在控制器中检查最后两行 @ opponents.any?这样你就不会在没有对手的情况下搞砸了。

最后第三行你使用的是current_user.id:

 @opponents = @contest.entries.where.not(user_id: current_user.id).sort_by { |entry| -entry.total_points}

但是在您的规范中,您使用 curre_user

定义了条目
let!(:my_entry) { create(:entry, user_id: user.id, contest_id: contest.id) } 

并且您在此处搜索不包含 current_user.id 的所有条目:

@opponents = @contest.entries.where.not(user_id: current_user.id).sort_by { |entry| -entry.total_points}

对于您的规范,您应该创建其他用户并为他创建一个条目。