Postgresql Rails 4控制器测试失败-ActiveRecord::AssociationTypeMismatch?
我正在用Postgres(0.18.3)、Rspec(3.1.0)和FactoryGirl(4.5.0)构建一个Rails(4.1.8)应用程序。我需要帮助对控制器测试进行故障排除,该测试引发由FactoryGirl对象引起的Active Record::AssociationTypeMismatch错误 以下是我的健身目标控制器索引操作:Postgresql Rails 4控制器测试失败-ActiveRecord::AssociationTypeMismatch?,postgresql,ruby-on-rails-4,activerecord,rspec,factory-bot,Postgresql,Ruby On Rails 4,Activerecord,Rspec,Factory Bot,我正在用Postgres(0.18.3)、Rspec(3.1.0)和FactoryGirl(4.5.0)构建一个Rails(4.1.8)应用程序。我需要帮助对控制器测试进行故障排除,该测试引发由FactoryGirl对象引起的Active Record::AssociationTypeMismatch错误 以下是我的健身目标控制器索引操作: def index @fitness_goals = @member.fitness_goals.order(:start_date) end
def index
@fitness_goals = @member.fitness_goals.order(:start_date)
end
这是我对健身目标控制器索引操作(健身\u目标\u控制器\u规范.rb)的设置和测试:
rspec错误和回溯:
1) FitnessGoalsController GET index assigns all fitness goals as @member.fitness_goals
Failure/Error: @fitness_goal = FactoryGirl.create(:fitness_goal, member: @member)
ActiveRecord::AssociationTypeMismatch:
Target(#62887900) expected, got String(#8489780)
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/association.rb:216:in `raise_on_type_mismatch!'
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/collection_association.rb:356:in `block in replace'
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/collection_association.rb:356:in `each'
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/collection_association.rb:356:in `replace'
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/collection_association.rb:41:in `writer'
# .rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/associations/builder/association.rb:118:in `targets='
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `public_send'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:12:in `object'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# .rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/notifications.rb:161:in `instrument'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# .rvm/gems/ruby-2.1.5/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/controllers/fitness_goals_controller_spec.rb:44:in `block (2 levels) in <top (required)>'
结束
健身目标参数:
def fitness_goal_params
params.require(:fitness_goal).permit(:goal_list_id, :timeframe_id, :start_date, :end_date, { target_ids: [] }, { activity_ids: [] }, :notes, :member_id, :trainer_id)
end
健身目标工厂:
FactoryGirl.define do
factory :fitness_goal do
association :goal_list
association :timeframe
start_date Date.current
end_date Date.current + 30
targets ["Lose Fat", "Reduce caloric intake by x%"]
activities ["Walk x steps a day", "Climb x floors a day", "Run x miles a day"]
association :member
association :trainer
notes 'This is a sample note.'
end
end
我做错了什么?应用程序代码在开发和生产环境中都能正常工作。看来问题出在我为FactoryGirl对象设置的某个地方。实现HABTM关联是打破控制器测试的原因。如何解决该问题并使控制器测试再次通过?谢谢你的帮助 您需要更改工厂以将目标对象而不是字符串传递给该关联。因此,您需要创建目标对象(或者找到它们,如果它们已经存在的话)。改变 到 我使用的标签是“field”,因为我不确定该字段的名称,所以只需使用它的名称即可
create_table "fitness_goals_targets", id: false, force: true do |t|
t.integer "fitness_goal_id"
t.integer "target_id"
def fitness_goal_params
params.require(:fitness_goal).permit(:goal_list_id, :timeframe_id, :start_date, :end_date, { target_ids: [] }, { activity_ids: [] }, :notes, :member_id, :trainer_id)
end
FactoryGirl.define do
factory :fitness_goal do
association :goal_list
association :timeframe
start_date Date.current
end_date Date.current + 30
targets ["Lose Fat", "Reduce caloric intake by x%"]
activities ["Walk x steps a day", "Climb x floors a day", "Run x miles a day"]
association :member
association :trainer
notes 'This is a sample note.'
end
end
targets ["Lose Fat", "Reduce caloric intake by x%"]
targets { [create(:target, field: "Lose Fat"), create(:target, field: "Reduce caloric intake by x%")] }