期望“对象”接收不到按预期方式工作的消息

Bit*_*ise 3 ruby testing rspec ruby-on-rails

我有这个规格:

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

  expect(GoogleCalendarApi::V1::Client).to receive(:get_cfm)
end
Run Code Online (Sandbox Code Playgroud)

我已经调试了在此测试过程中要调用的消息,并且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
Run Code Online (Sandbox Code Playgroud)

mrz*_*asa 5

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

  expect(GoogleCalendarApi::V1::Client).to receive(:get_cfm)
  fetch_cfm_data
Run Code Online (Sandbox Code Playgroud)

在rspec-mocksdocs中查看更多信息。