Creating test objects in RSpec with FactoryGirl fails with Nested Attributes
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!
Don't set the exercise id. Let SQLite handle the id's for you.
factory :performed_exercise do
performed_set
end
精彩评论