Ruby on rails 期望;“对象”;接收未按预期工作的消息

Ruby on rails 期望;“对象”;接收未按预期工作的消息,ruby-on-rails,ruby,testing,rspec,Ruby On Rails,Ruby,Testing,Rspec,我有这个规格: 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肯定会被调用,因为我可以在测试运行期间窥探该方法的内部。为什么我的测试返回这个失败 Failure/

我有这个规格:

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
肯定会被调用,因为我可以在测试运行期间窥探该方法的内部。为什么我的测试返回这个失败

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

请参阅。

中的详细信息,或者您可以使用
spy
expect(spy)。要接收(消息)
请将您的操作(获取数据)移动到expect行之后,