【发布时间】:2017-06-10 11:07:15
【问题描述】:
当我不使用 factory_girl 时
我可以在我的 rspec 测试中创建对象
u = User.create(email: 'as@wewe.com', password: 'asdqweqweqe', admin: true, firstname: 'qwe', lastname: 'wer', grade: 5, section: 'w')
expect(u.errors[:email]).to_not be_empty
expect(u.errors[:role]).to_not be_empty
这样我可以检查验证错误,即
expect(u.errors[:role]).to_not be_empty
但如果我使用工厂女孩
factory :user_no_role, class: User do
email 'asd@we.com'
password 'asasdasdasd'
admin true
firstname 'qwe'
lastname 'wer'
grade 5
section 'w'
end
it 'role should be present' do
u = create(:user_no_role)
expect(u.errors[:role]).to_not be_empty
end
我收到以下错误
User role should be present
Failure/Error: u = create(:user_no_role)
ActiveRecord::RecordInvalid:
Validation failed: Role can't be blank
factory_girl 创建方法会抛出错误吗?如果是这样,我如何使用上面的 rspecs 测试验证错误?谢谢!
【问题讨论】:
标签: ruby-on-rails factory-bot rspec-rails