预期 "Object" 接收消息未按预期工作

Expect "Object" to receive message not working as expected

我有这个规格:

it 'makes a request to google if redis is empty' do
  fetch_cfm_data

  expect(GoogleCalendarApi::V1::Client).to receive(:get_cfm)
end

我已经调试了在我的测试过程中被调用的消息,并且 GoogleCalendarApi::V1::Client.get_cfm 肯定被调用了,因为我可以在测试 运行 期间窥探该方法的内部。为什么我的测试 return 失败了?

Failure/Error: expect(GoogleCalendarApi::V1::Client).to receive(:get_cfm)

       (GoogleCalendarApi::V1::Client (class)).get_cfm(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments

需要先设置期望值,然后调用被测方法:

  expect(GoogleCalendarApi::V1::Client).to receive(:get_cfm)
  fetch_cfm_data

rspec-mocks docs中查看更多内容。