简体   繁体   English

RSpec:如何编写单元测试用例以接收在私有方法中引发的异常

[英]RSpec: How to write unit test case to receive an exception which is getting raised in private method

I have implemented Optimistic Locking for Race Condition. 我已经实现了竞争条件的乐观锁定。 For that, I added an extra column lock_version in the Product. 为此,我在产品中添加了额外的列lock_version Method: recalculate is calling private method_1 then it saves ( save! ) the product. 方法: recalculate正在调用私有方法method_1然后保存( save! )产品。 I can't use save! 我不能使用save! in private method_1 , because it will fail the other things. 在private method_1 ,因为它将使其他操作失败。 I don't want to restructure the business logic. 我不想重组业务逻辑。

#Product: Model's new field:
    #  lock_version                       :integer(4)      default(0), not null

def recalculate
  method_1
  self.save!
end

private
def method_1
    begin
      ####
      ####
      if self.lock_version == Product.find(self.id).lock_version
         Product.where(:id => self.id).update_all(attributes)
      else
         raise ActiveRecord::StaleObjectError.new(self, "test")
      end
    rescue ActiveRecord::StaleObjectError => e
        if tries < 3
           tries += 1
           sleep(1 + tries)
           self.reload
           retry
        else
           raise Exception.new(timeout.inspect)
        end
    end
end

Rspec Test case: Rspec测试用例:

  it 'if car is updated then ActiveRecord::StaleObjectError should be raised' do
    prod_v1 =Product.find(@prod.id)
    prod_v2 = Car.find(@prod.id)
    prod_v1.recalculate
    prod_v1.reload  # will make lock_version of prod_v1 to 1

    prod_v2.recalculate # howvever lock_version of prod_v2 is still 0.

    expect(car_v2).to receive(:method1).and_raise(ActiveRecord::StaleObjectError)
  end

When I try to write above the test case, then it should raise an Exception ActiveRecord::StaleObjectError . 当我尝试在测试用例上方书写时,它应该引发Exception ActiveRecord::StaleObjectError However, I am getting an error like 但是,我收到类似的错误

 Failure/Error: expect(car_v2).to receive(:set_total_and_buckets_used).and_raise(ActiveRecord::StaleObjectError)
 ArgumentError:
   wrong number of arguments (0 for 2)

You could write something like: 您可以这样写:

expect(ActiveRecord::StaleObjectError).to receive(:new).and_call_original

Because you are rescue-ing the exception 因为您正在挽救异常

Make sure to check https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers 确保检查https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

expect(car_v2).to receive(:method1).and_raise(ActiveRecord::StaleObjectError)

Means that when car_v2 receives the method1 you won't call it but you'll raise an exception of type ActiveRecord::StaleObjectError . 意味着当car_v2收到method1您不会调用它,但是会引发ActiveRecord::StaleObjectError类型的异常。 This is why you also receive the ArgumentError 这就是为什么您还会收到ArgumentError的原因

With rspec you can check that a specific code raises an error (that is not handled in your case it is handled -> rescue...) like this: 使用rspec,您可以检查特定代码是否引发错误(在您的情况下不会处理->救援...),如下所示:

expect { my_cool_method }.to raise_error(ErrorClass)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM