RSpec - 检查方法是否被进一步向下(向上?)调用堆栈调用
RSpec - Check if method was called further down(up?) the call stack
我一直在使用内置的 Rails 测试套件,现在正在使用 RSpec 进行测试。我试图弄清楚是否在调用另一个方法的过程中调用了一个方法。
例如,我的 update_service.call 方法被一个对象调用。 update_service 调用 manage_service.call 方法。我正在尝试测试 manage_service.call 方法是否被调用过。
expect(@update_service).to receive(:call).with(object).at_least(:once)
expect(@manage_service).to receive(:call).with(object.id).at_least(:once)
@update_service.call(encounter)
使用此设置,出现以下故障:
Failures:
1) UpdateService should call manage_service
Failure/Error: expect(@manage_service).to receive(:call).with(object.id).at_least(:once)
(#<ManageService:0x00007ffaf8c76190 @sdi=ServiceDI>).call(1)
expected: at least 1 time with arguments: (1)
received: 0 times
# ....
我不确定我在这里遗漏了什么。
RSpec 允许您使用 any_instance_of
组匹配器来进行设置。
如果您需要执行这种特定类型的测试并且不能通过 DI 手动指定实例,这将很有用。
expect_any_instance_of(Widget).to receive(:name).and_return("Wobble")
见here
我一直在使用内置的 Rails 测试套件,现在正在使用 RSpec 进行测试。我试图弄清楚是否在调用另一个方法的过程中调用了一个方法。 例如,我的 update_service.call 方法被一个对象调用。 update_service 调用 manage_service.call 方法。我正在尝试测试 manage_service.call 方法是否被调用过。
expect(@update_service).to receive(:call).with(object).at_least(:once)
expect(@manage_service).to receive(:call).with(object.id).at_least(:once)
@update_service.call(encounter)
使用此设置,出现以下故障:
Failures:
1) UpdateService should call manage_service
Failure/Error: expect(@manage_service).to receive(:call).with(object.id).at_least(:once)
(#<ManageService:0x00007ffaf8c76190 @sdi=ServiceDI>).call(1)
expected: at least 1 time with arguments: (1)
received: 0 times
# ....
我不确定我在这里遗漏了什么。
RSpec 允许您使用 any_instance_of
组匹配器来进行设置。
如果您需要执行这种特定类型的测试并且不能通过 DI 手动指定实例,这将很有用。
expect_any_instance_of(Widget).to receive(:name).and_return("Wobble")
见here