【发布时间】:2018-05-03 16:55:25
【问题描述】:
这里我们有一个带有多个验证的 Customer 模型,其中一个验证访问外部服务以验证此人是否存在。
我们在这里测试所有验证:
describe Customer do
it 'is not valid without a name' do
customer = build_stubbed(:customer, name: nil)
customer.valid?
asserts...
end
it 'is not valid without a full name' do
customer = build_stubbed(:customer, name: 'Test')
customer.valid?
asserts...
end
if 'should check if user really exists' do
customer = build_stubbed(:customer, doc_id: '00000000')
customer.valid?
asserts....
end
other tests...
最后一项使用 doc_id 测试外部服务验证以检查此人是否存在,但这里每个测试都会运行所有验证,所以我应该使用 before stub_request 服务请求还是在每个测试中 stub 它?
【问题讨论】:
标签: ruby-on-rails validation rspec stub