Questions tagged [webmock]

Library for stubbing and setting expectations on HTTP requests in Ruby.

Library for stubbing and setting expectations on HTTP requests in Ruby.

Features

  • Stubbing HTTP requests at low http client lib level (no need to change tests when you change HTTP library)
  • Setting and verifying expectations on HTTP requests
  • Matching requests based on method, URI, headers and body
  • Smart matching of the same URIs in different representations (also encoded and non encoded forms)
  • Smart matching of the same headers in different representations.
  • Support for Test::Unit
  • Support for RSpec 1.x and RSpec 2.x
  • Support for MiniTest

Supported HTTP libraries

  • Net::HTTP and libraries based on Net::HTTP (i.e RightHttpConnection, REST Client, HTTParty)
  • HTTPClient
  • Patron
  • EM-HTTP-Request
  • Curb (currently only Curb::Easy)
  • Typhoeus (currently only Typhoeus::Hydra)
  • Excon

Supported Ruby Interpreters

  • MRI 1.8.6
  • MRI 1.8.7
  • MRI 1.9.1
  • MRI 1.9.2
  • MRI 1.9.3
  • REE 1.8.7
  • JRuby
  • Rubinius

Source: https://github.com/bblimke/webmock

166 questions
3
votes
1 answer

Stubbing 3rd party API calls in Rails integration tests

I have a TwitterAPI class defined, which interacts with Twitter's API. The methods in the module make two separate API calls. So I have two WebMock stub_requests defined in before blocks in my unit tests for the TwitterAPI class. I am writing…
user3809888
  • 377
  • 1
  • 6
  • 23
3
votes
3 answers

How can I fake a response to Capybara/poltergeist using webmock?

I'm testing a webscraper and I'd like to use Webmock to deliver fake websites for faster testing. When I mock a website, Ruby's native HTTP library works fine, but Capybara doesn't seem capable of receiving the mocked response. I know that webmock…
pixelpax
  • 1,435
  • 13
  • 22
3
votes
1 answer

Webmock: how to fake a gzipped response?

I don't have much experience with stubbing and am having issues with requests to Braintree using webmock and braintree-rails. spec/spec_helper.rb RSpec.configure do |config| config.include(ConnectionHelper) config.before(:each) do …
mind.blank
  • 4,820
  • 3
  • 22
  • 49
3
votes
0 answers

Webmock and VCR, allow Http Connections if there is no cassette

I have a problem, I can run a test that uses vcr on its own and it works, it creates the cassette and it uses that on the next test. Great. The problem is when I run all my tests together this particular test fails, because webmock disables http…
TheWebs
  • 12,470
  • 30
  • 107
  • 211
3
votes
1 answer

Stubing google places call selenium vs capybara-webkit

I am using Capybara with selenium AND/OR capybara-webkit driver and Webmock as a stubbing framework. When I run my integration tests for google maps (places) autocomplete input using selenium (firefox) the browser makes a few real calls to google…
luigi7up
  • 5,779
  • 2
  • 48
  • 58
3
votes
2 answers

Test oauth2 get_token with rspec

I'm creating a controller spec for the get_token part of an oauth2 authentication. At this point the user has authorized my app and I need to generate and save the token and other information. Rspec fails with a somewhat cryptic…
brodney
  • 1,176
  • 2
  • 14
  • 29
3
votes
1 answer

RSpec before suite not being run

I'm trying to stub any external API calls in my test suite, but the before(:suite) is never executed. Webmock always reports that I need to stub the maps.googleapis.com even though no tests have been run yet (no green dots, no red…
3
votes
1 answer

Webmock not responding to stub_request

In features/support/webmock.rb, I have stub_request(:get, /(http\:\/\/catalog\.viglink\.com\/vigcatalog\/products\.xml\?include_identifiers=true&key=.*&keyword_upc=628586348097&results_per_page=20)/). with(:headers => {'Accept'=>'*/*; q=0.5,…
Hadiyah Mujhid
  • 177
  • 1
  • 2
  • 10
3
votes
2 answers

Using webmock to stub partial headers

I am creating tests using webmock. I want to test that a particular header field is set, but I don't care about other header fields. When I use this: stub_request(:get, "https://myhost.com/api"). with(:headers => {:user_agent => 'Custom user…
Chris
  • 443
  • 8
  • 16
3
votes
2 answers

How to use WebMock to mock Paperclip call in a Sinatra app?

This code works fine without WebMock. Raising an exception: Paperclip::AdapterRegistry::NoHandlerError: No handler found for # # ./spec/support/api_mock.rb:34:in…
B Seven
  • 44,484
  • 66
  • 240
  • 385
2
votes
1 answer

In Rails 6 and webmock, how do I write a regular expression to capture any 'User-Agent' header?

I’m using Rails 6 and web mock 3.14.0. I mock a particular outbound request like so stub_request(:post, "https://#{APP_CONFIG.vendor[Rails.env][:api].domain}/api/start“). with( headers: { 'Accept'=>'application/json', …
Dave
  • 15,639
  • 133
  • 442
  • 830
2
votes
1 answer

WebMock: How to list (dump) all mocked responses?

If you make a web request in a test that isn't registered, WebMock will raise an exception, suggest how you can mock it, and list all the previously registered mocks: I'd like to be able to dump all the registered mocks, just like the green-circled…
David Hempy
  • 5,373
  • 2
  • 40
  • 68
2
votes
1 answer

webmock to_return with status 400 and also a body

I'm writing tests using webmock, I need to simulate the remote api to return a 400 status, with body content, if I just change the status from 200 to 400, with a body, the status I receive becomes 200. let(:stub_result) { { result: 'failed',…
Don Giulio
  • 2,946
  • 3
  • 43
  • 82
2
votes
1 answer

How can I get WebMock to match a body containing specific XML data using hash_including?

I have a Ruby (2.6.5) on Rails (5.2.3) application which is already successfully using Savon (2.12) to interact with FlightAware's FlightXML 2 API via SOAP/WSDL. However, I'm running into an issue while attempting to write Minitest tests for this…
bogardpd
  • 287
  • 1
  • 10
2
votes
1 answer

Stubbing `session_id` in Rails Integration Tests

I have a question about Rails Integration Tests. I have this app that talks to this other internal app. The app makes a request to internal_app, and one of the params for that request involves the session_id, stored in the session cookie. For a…
mc92
  • 475
  • 5
  • 10
1 2
3
11 12