【发布时间】:2017-07-17 20:13:19
【问题描述】:
我有以下任务模型:
class Task < ApplicationRecord
validates :body, presence: true, length: { minimum: 10 }
validates :complete, presence: true
end
及以下FactoryGirl对象创建代码:
FactoryGirl.define do
factory :incomplete_task, class: :Task do
body { Faker::Pokemon.name + Faker::Pokemon.name }
complete false
factory :complete_task do
complete true
end
end
end
在我的任务控制器测试中,我有:
describe '#update' do
it 'toggles completion' do
incomplete_task = create :incomplete_task
toggle_completion(incomplete_task)
expect(incomplete_task.complete).to be_true
end
end
但是,这会失败,因为 FG 创建的任务对象中缺少字段“完成”:
Failures:
1) TasksController#update toggles completion
Failure/Error: incomplete_task = create :incomplete_task
ActiveRecord::RecordInvalid:
Validation failed: Complete can't be blank
这里发生了什么?我正确设置了完整的属性,并且身体检查正常。这里也是任务架构:
# Table name: tasks
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# body :text
# complete :boolean
【问题讨论】: