4

I am testing a Ruby on Rails project using WebMock and VCR. I would like to assert that a request is made to the correct host, but I can't find an example of how to do that. Here's some pseudo-code for what I'm trying to do:

VCR.use_cassette('example.com request') do
  subject.domain = 'example.com'
  expect { subject.get('/something') }.to make_a_get_request_for('http://example.com/something')
end
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 1
    VCR is awesome for recording/playing, but why do you need VCR then if you just want to set an expectation on a request? – Bartosz Blimke Feb 18 '14 at 20:10
  • 2
    Good point. I think I was trying to test too many things in one test. I can create a separate test that does not use VCR to test the request with WebMock. – Andrew Feb 18 '14 at 23:54

1 Answers1

0

Assuming your VCR is recording to YAML (the default, I believe?), this one-liner returns an array of HTTP URIs that were called during the current cassette block:

YAML.load_file(VCR.current_cassette.file)["http_interactions"].pluck("request").pluck("uri")

E.g.,

YAML.load_file(VCR.current_cassette.file)["http_interactions"].pluck("request").pluck("uri")
[
    [0] "https://stackoverflow.com/wayland-project/libinput/e0008d3d/tools/libinput-debug-gui.c",
    [1] "https://stackoverflow.com/wayland-project/libinput"
]

You can use an array matching method of your choice to assert the expected request URI from there.

Other members of the request object you might want to pluck instead of uri:

{
         "method" => "get",
            "uri" => "https://stackoverflow.com/adomain",
           "body" => {
            "encoding" => "US-ASCII",
              "string" => ""
        },
        "headers" => {
           "Accept" => [
          ...
        }
}
wbharding
  • 4,213
  • 2
  • 30
  • 25