【问题标题】:How to stub a model with a validation from external service如何使用来自外部服务的验证来存根模型
【发布时间】: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


    【解决方案1】:

    您应该使用context 来指定哪个测试使用了stup,哪个不使用。例如,建议here。这样,您将能够拥有一个方法被存根的上下文,并在另一个上下文中使用外部服务测试验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多