用Cucumber特性中的VCR测试Rails3后台作业(API-Request)的响应



我有一个Rails3后台作业(delayed_job),它向他们的API发送hipchat/Campfire消息,我想检查我的Cucumber功能中的响应。有没有办法获得VCR录制的最后一个HTTP响应?

功能看起来像这个

    @vcr
    Scenario: Send hipchat message when task created
      Given an hipchat_sample integration exists with app: app "teamway"
      When I create an "ActionMailer::Error" task to "Teamway"
      And all jobs are worked off # invoke Delayed::Worker.new.work_off
      Then a hipchat message should be sent "ActionMailer::Error"

在我的步骤定义中,我想检查响应主体:

    Then /^a hipchat message should be sent "(.*?)"$/ do |arg1|
      # Like this:
      # VCR::Response.body.should == arg1
    end

录像机已经记录了请求和响应,但我不知道如何处理。我想到了一些类似于用皮克尔的脚步捕捉电子邮件的东西。有人知道怎么做吗?

我使用rails 3.2.8、cucumber rails 1.3和vcr 2.2.4(带webmock)。

致以最良好的问候Torsten

您可以使用VCR.current_cassette获取当前盒式磁带,然后询问它以获取您要查找的[VCR::HTTPInteraction][1]对象,但这会有点复杂——VCR盒式磁带将新录制的HTTP交互与可播放的交互和已播放的交互分开存储。。。因此,您需要一些复杂的条件来确保测试在录制和回放时都能正常工作。

相反,我建议您使用after_http_request挂钩:

module HipmunkHelpers
  extend self
  attr_accessor :last_http_response
end
Before { HipmunkHelpers.last_http_response = nil }
VCR.configure do |c|
  c.after_http_request(lambda { |req| URI(req.uri).host == 'hipmunk.com' }) do |request, response|
    HipmunkHelpers.last_http_response = response
  end
end

然后,在黄瓜步骤中,您可以访问HipmunkHelpers.last_http_response

有关after_http_request挂钩的更多详细信息,请查看调味品文档。

最新更新