Ruby on rails 对具有嵌套资源和关联FactoryGirl对象的控制器的RSpec测试失败
我正在构建一个学习Rails和测试的项目,并且正在努力解决指导嵌套资源的控制器的RSpec测试中的错误。我的代码在浏览器中按预期工作。我相信这个问题与我的测试设置和FactoryGirl对象的关联有关。我需要帮助排除故障并修复控制器规格 这是有氧运动控制器.rbRuby on rails 对具有嵌套资源和关联FactoryGirl对象的控制器的RSpec测试失败,ruby-on-rails,testing,rspec,tdd,factory-bot,Ruby On Rails,Testing,Rspec,Tdd,Factory Bot,我正在构建一个学习Rails和测试的项目,并且正在努力解决指导嵌套资源的控制器的RSpec测试中的错误。我的代码在浏览器中按预期工作。我相信这个问题与我的测试设置和FactoryGirl对象的关联有关。我需要帮助排除故障并修复控制器规格 这是有氧运动控制器.rb class CardioExercisesController < ApplicationController # :get_member is defined in the private method at the bott
class CardioExercisesController < ApplicationController
# :get_member is defined in the private method at the bottom of this file,
# and takes the member_id provided by the routing and
#converts it to a @member object.
before_action :get_member
# GET member/1/cardio_exercises
# GET member/1/cardio_exercises.json
def index
@cardio_exercises = @member.cardio_exercises
end
# GET member/1/cardio_exercises/1
# GET member/1/cardio_exercises/1.json
def show
cardio_exercise = @member.cardio_exercises.find(params[:id])
end
# GET member/1/cardio_exercises/new
def new
@member = Member.find(params[:member_id])
@cardio_exercise = @member.cardio_exercises.build
end
# GET member/1/cardio_exercises/1/edit
def edit
@cardio_exercise = @member.cardio_exercises.find(params[:id])
end
# POST member/1/cardio_exercises
# POST member/1/cardio_exercises.json
def create
@cardio_exercise = @member.cardio_exercises.build(cardio_exercise_params)
if @cardio_exercise.save
flash[:success] = "Cardio exercise was successfully created."
redirect_to member_cardio_exercises_path(@member)
else
render 'new'
end
end
# PATCH/PUT member/1/cardio_exercises/1
# PATCH/PUT member/1/cardio_exercises/1.json
def update
@cardio_exercise = @member.cardio_exercises.find(params[:id])
if @cardio_exercise.update(cardio_exercise_params)
flash[:success] = "Cardio exercise was successfully updated."
redirect_to member_cardio_exercises_path(@member)
else
render 'edit'
end
end
# DELETE member/1/cardio_exercises/1
# DELETE member/1/cardio_exercises/1.json
def destroy
@cardio_exercise = @member.cardio_exercises.find(params[:id])
@cardio_exercise.destroy
respond_to do |format|
format.html { redirect_to (member_cardio_exercises_path(@member)), notice: 'Cardio exercise was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# The get_member action converts the member_id given by the routing
# into an @member object, for use here and in the view.
def get_member
@member = Member.find(params[:member_id])
end
def cardio_exercise_params
params.require(:cardio_exercise).permit(:title, :duration, :calories_burned, :date, :member_id)
end
end
这些是相关的工厂:
FactoryGirl.define do
factory :cardio_exercise do
title "My cardio exercise"
duration 30
calories_burned 300
date "2014-11-15"
association :member
end
end
FactoryGirl.define do
factory :member do
first_name {Faker::Name.first_name}
last_name {Faker::Name.last_name}
age 21
height 75
weight 195
goal "fffff" * 5
start_date "2014-11-15"
end
end
routes.rb包含:
资源:各位议员有
资源:有氧运动
结束
members.rb包含:
有很多:有氧运动,:依赖=>:破坏
cardio_exercises.rb包含:
属于:成员
Rspec故障/错误:
1) CardioExercisesController GET show assigns the requested cardio_exercise as @member.cardio_exercise
Failure/Error: get :show, { :member_id => @member, :id => @cardio_exercise }
ActiveRecord::RecordNotFound:
Couldn't find CardioExercise with 'id'=25 [WHERE "cardio_exercises"."member_id" = $1]
2) CardioExercisesController POST create with valid params redirects to the created cardio_exercise
Failure/Error: expect(response).to redirect_to(CardioExercise.last)
NoMethodError:
undefined method `cardio_exercise_url' for #<CardioExercisesController:0x00000008bba960>
3) CardioExercisesController DELETE destroy destroys the requested cardio_exercise
Failure/Error: delete :destroy, { :member_id => @member, :id => @cardio_exercise }
ActiveRecord::RecordNotFound:
Couldn't find CardioExercise with 'id'=34 [WHERE "cardio_exercises"."member_id" = $1]
4) CardioExercisesController DELETE destroy redirects to the cardio_exercises list
Failure/Error: delete :destroy, { :member_id => @member, :id => @cardio_exercise }
ActiveRecord::RecordNotFound:
Couldn't find CardioExercise with 'id'=35 [WHERE "cardio_exercises"."member_id" = $1]
1)心脏锻炼控制器GET show将请求的心脏锻炼分配为@member.cardio\u exercise
失败/错误:get:show,{:member\u id=>@member,:id=>@cardio\u exercise}
ActiveRecord::RecordNotFound:
找不到“id”=25[其中“cardio_练习”。“member_id”=1]
2) CardioExercisesController使用有效参数后创建重定向到已创建的cardio_练习
失败/错误:期望(响应)。将_重定向到(CardioExercise.last)
命名错误:
未定义的“有氧运动”方法#
3) Cardioexercise控制器DELETE destroy销毁请求的有氧运动
失败/错误:删除:销毁,{:member\u id=>@member,:id=>@cardio\u exercise}
ActiveRecord::RecordNotFound:
找不到“id”=34[其中“cardio_练习”。“member_id”=1]
4) CardioExerciseController DELETE destroy重定向到cardio_练习列表
失败/错误:删除:销毁,{:member\u id=>@member,:id=>@cardio\u exercise}
ActiveRecord::RecordNotFound:
找不到“id”=35[其中“cardio_练习”。“member_id”=1]
我认为记录未发现错误表明会员与有氧运动模型之间的关联存在问题。控制器没有通过id找到有氧运动。我在设置RSpec时错过了什么?修复设置的最佳方法是什么
“未定义的方法”错误似乎是由于我调用了心脏锻炼的最后一个方法引起的。我将发布到示例中的create方法。有氧运动是最重要的课程。有人能解释为什么那个调用会触发错误,以及如何修复它吗
谢谢你的帮助 创建两个独立的对象/记录:会员和有氧运动。 您应该将
@member
传递到工厂有氧运动
以连接它们
@member = FactoryGirl.create(:member)
@cardio_exercise = FactoryGirl.create(:cardio_exercise, member: @member)
PS当您创建cardio\u exercise
而不设置member
时,工厂将在表成员中创建新记录,并使用此新记录分配cardio\u exercise
更新
关于“获取索引”
您创建的@member
没有任何相关的有氧运动
。您后来添加了它们,对象@member
对此一无所知。您应该重新加载对象以从数据库中获取数据
expect(assigns(:cardio_exercises)).to eq(@member.reload.cardio_exercises)
有时我会将关系转换为数组并对结果进行排序,以避免顺序不同时测试失败谢谢回复!“显示”、“新建”、“编辑”和“删除”等级库现在正在通过。不幸的是,修改后的代码导致先前通过的“GET index”规范失败。它触发一个失败/错误:expect(分配(:cardio_练习))。对于eq(@member.cardio_练习),expected:#此外,创建后重定向规范仍然失败。改进了很多,但不明白为什么有些测试仍然失败..谢谢@gotva!添加重新加载工作;GET索引规范现在应该通过了。谢谢你抽出时间来帮助我。
expect(assigns(:cardio_exercises)).to eq(@member.reload.cardio_exercises)