使用 FactoryGirl 在 RSpec 中创建测试对象因嵌套属性而失败

发布于 2024-11-25 08:36:49 字数 942 浏览 4 评论 0原文

我有一个包含许多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2024-12-02 08:36:49

不要设置练习 ID。让 SQLite 为您处理 id。

factory :performed_exercise do
  performed_set
end

Don't set the exercise id. Let SQLite handle the id's for you.

factory :performed_exercise do
  performed_set
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文