Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用来自外部服务的验证来存根模型_Ruby On Rails_Validation_Rspec_Stub - Fatal编程技术网

Ruby on rails 如何使用来自外部服务的验证来存根模型

Ruby on rails 如何使用来自外部服务的验证来存根模型,ruby-on-rails,validation,rspec,stub,Ruby On Rails,Validation,Rspec,Stub,在这里,我们有一个具有多个验证的客户模型,其中一个验证访问一个外部服务以验证此人是否存在 在这里,我们测试所有验证: 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

在这里,我们有一个具有多个验证的客户模型,其中一个验证访问一个外部服务以验证此人是否存在

在这里,我们测试所有验证:

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\u id测试外部服务验证,以检查该人员是否存在,但在这里,每个测试都运行所有验证,因此我应该在每个测试之前存根请求服务请求,还是在每个测试中存根请求服务请求?

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