2

我编写了一个控制器函数,该函数运行 curl 命令以将消息发送到 3rd 方服务。我想测试此功能以确认我的消息已发送。但是,当我使用 Webmock 时,它不会阻止命令。我可以做些什么来验证消息是在 rspec 测试中发送的?

4

2 回答 2

1

我最终使用了Typhoeus,它允许我在 ruby​​ 中发送自定义、curl样式的 Web 请求。我也可以用它来存根调用。rspec 测试内容如下所示:

  it 'correctly prepares a new Typhoeus request and runs it' do
    Typhoeus::Request.should_receive(:new).with(<request>).and_call_original
    Typhoeus::Request.any_instance.should_receive(:run).and_call_original
    object.method
  end
于 2013-08-15T16:17:23.537 回答
0

您可以使用 ruby​​ 进行 HTTP 调用,也可以将 curl 命令包装在可以设置期望的模块中:

module Curl
  def self.perform(url)
    `curl -s "#{url}"`
  end
end

Curl.stub(perform: 'http response')
perform_some_action
expect(Curl).to have_received(:perform).with(the_url)
于 2013-08-07T12:39:56.223 回答