使用 FactoryGirl 在 RSpec 中创建测试对象因嵌套属性而失败
我有一个包含许多 PerformedExercises 的 Workout 模型,其中有许多 PeformedSets。我无法让它在我的测试中构建一个对象,并且我不确定它是 SQLite3 还是其他东西(它在测试环境之外运行良好)。
我有以下工厂:
FactoryGirl.define do
factory :workout do
title 'workout one'
performed_exercise
end
factory :performed_exercise do
exercise_id '2'
performed_set
end
factory :performed_set do
set_number '1'
end
end
我的 RSpec 测试看起来像这样(我已经使它非常简单,以便排除测试中的任何其他问题):
it "is causing me to lose hair" do
wrkt = FactoryGirl.build(:workout)
end
当我运行测试时,我收到以下错误消息:
Failure/Error: wrkt = FactoryGirl.build(:workout)
ActiveRecord::StatementInvalid:
SQLite3::ConstraintException: constraint failed:
INSERT INTO "performed_sets" ("created_at", "notes", "performed_exercise_id", "reps", "set_number", "updated_at", "weight")
VALUES (?, ?, ?, ?, ?, ?, ?)
任何帮助都会非常感谢!
I have a Workout model that has many PerformedExercises, which has many PeformedSets. I can't get it to build an object in my test and I am not sure if it's SQLite3, or something else (it works fine outside of the testing environment).
I have the following factories:
FactoryGirl.define do
factory :workout do
title 'workout one'
performed_exercise
end
factory :performed_exercise do
exercise_id '2'
performed_set
end
factory :performed_set do
set_number '1'
end
end
My RSpec test looks like so (I've made it real simple so as to rule out any other issues inside the test):
it "is causing me to lose hair" do
wrkt = FactoryGirl.build(:workout)
end
When I run the test, I get the following error message:
Failure/Error: wrkt = FactoryGirl.build(:workout)
ActiveRecord::StatementInvalid:
SQLite3::ConstraintException: constraint failed:
INSERT INTO "performed_sets" ("created_at", "notes", "performed_exercise_id", "reps", "set_number", "updated_at", "weight")
VALUES (?, ?, ?, ?, ?, ?, ?)
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要设置练习 ID。让 SQLite 为您处理 id。
Don't set the exercise id. Let SQLite handle the id's for you.