Rails 和 FactoryBot - 无法启动用户模型,ActiveRecord::RecordInvalid
Rails and FactoryBot - unable to launch User model, ActiveRecord::RecordInvalid
我正在尝试将 FactoryBot 模型实施到我的 Rspec 测试中。这些模型看起来像这样:
FactoryBot.define do
factory :user do
first_name { "MyString" }
last_name { "MyString" }
inbox
outbox
end
end
FactoryBot.define do
factory :outbox do
user_id { 1 }
end
factory :inbox do
user_id { 1 }
end
end
当我在我的 Rspec 测试中调用 "patient = create(:user)" 时,我得到了这个错误:
Failure/Error: patient = create(:user)
ActiveRecord::RecordInvalid:
Validation failed: User must exist
为了提供更广阔的视野,这里是我的模型:
class User < ApplicationRecord
has_one :inbox
has_one :outbox
has_many :payments
has_many :messages
end
class Inbox < ApplicationRecord
belongs_to :user
has_many :messages
end
class Outbox < ApplicationRecord
belongs_to :user
has_many :messages
end
关于schema.rb的一部分:
create_table "users", force: :cascade do |t|
t.boolean "is_patient", default: true
t.boolean "is_doctor", default: false
t.boolean "is_admin", default: false
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "outboxes", force: :cascade do |t|
t.integer "user_id"
t.index ["user_id"], name: "index_outboxes_on_user_id"
end
create_table "inboxes", force: :cascade do |t|
t.integer "user_id"
t.index ["user_id"], name: "index_inboxes_on_user_id"
end
我这里FactoryBot中的User无法通过验证是哪里做错了?
尝试使用 after(:build)
factory :user do
first_name { "MyString" }
last_name { "MyString" }
after(:build) do |user|
user.inbox = create(:inbox, user: user)
user.outbox = create(:outbox, user: user)
end
end
希望对您有所帮助!
我正在尝试将 FactoryBot 模型实施到我的 Rspec 测试中。这些模型看起来像这样:
FactoryBot.define do
factory :user do
first_name { "MyString" }
last_name { "MyString" }
inbox
outbox
end
end
FactoryBot.define do
factory :outbox do
user_id { 1 }
end
factory :inbox do
user_id { 1 }
end
end
当我在我的 Rspec 测试中调用 "patient = create(:user)" 时,我得到了这个错误:
Failure/Error: patient = create(:user) ActiveRecord::RecordInvalid: Validation failed: User must exist
为了提供更广阔的视野,这里是我的模型:
class User < ApplicationRecord
has_one :inbox
has_one :outbox
has_many :payments
has_many :messages
end
class Inbox < ApplicationRecord
belongs_to :user
has_many :messages
end
class Outbox < ApplicationRecord
belongs_to :user
has_many :messages
end
关于schema.rb的一部分:
create_table "users", force: :cascade do |t|
t.boolean "is_patient", default: true
t.boolean "is_doctor", default: false
t.boolean "is_admin", default: false
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "outboxes", force: :cascade do |t|
t.integer "user_id"
t.index ["user_id"], name: "index_outboxes_on_user_id"
end
create_table "inboxes", force: :cascade do |t|
t.integer "user_id"
t.index ["user_id"], name: "index_inboxes_on_user_id"
end
我这里FactoryBot中的User无法通过验证是哪里做错了?
尝试使用 after(:build)
factory :user do
first_name { "MyString" }
last_name { "MyString" }
after(:build) do |user|
user.inbox = create(:inbox, user: user)
user.outbox = create(:outbox, user: user)
end
end
希望对您有所帮助!