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
2
votes
0 answers

Stub request when request body is different

Hi I am working on RoR project with ruby-2.5.0 and Rails 5. I am stubbing an http request with stub_request. This http request is basically a mailjet api request to send email. So, I can not just reuse the snippet, rspec spits out to me, because…
awsm sid
  • 595
  • 11
  • 28
2
votes
1 answer

rspec webmock to_raise error not raising error

I have the following code snippet and test class Service def self.status(base_uri = 'localhost', basic_auth = {}) base_uri = "#{ base_uri }/status" response = HTTParty.get(base_uri, basic_auth: basic_auth) rescue StandardError => e …
Mark
  • 5,994
  • 5
  • 42
  • 55
2
votes
0 answers

Cucumber on Rails - stub only one request with local HTML file

I don't understand how to setup webmock (or any stubbing library) to stub only specific requests to a particular url (www.example.com). I am doing Cucumber tests on a browser and I want to allow connection to any site except for those requests I…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
2
votes
1 answer

Rails WebMock against XML request

I am having a hard time figuring out how to match stub_request with XML requests using webmock. Request:
chrisandrew.cl
  • 867
  • 14
  • 30
2
votes
0 answers

VCR cassette with empty body returns nil instead instead of empty string

I have an API that I'm trying to write some specs on and mock out the requests using the VCR gem. The issue I'm having is that when the API returns an empty body, Ruby interprets this as a empty string. However when I use VCR to generate a cassette…
Travis
  • 13,311
  • 4
  • 26
  • 40
2
votes
2 answers

Regex with webmock in aws-sdk not working

I'm trying to use webmock with rspec to stub out requests to Aws but I can't seem to get the regex to work for SQS polling. If I run rspec, webmock generates a 'correct' stub for me to use in a before(:each) block, in my spec_helper.rb like this: …
Adam
  • 344
  • 1
  • 15
2
votes
1 answer

Stubbing a 500 error

I am having an issue with webmocks stubbing. This is a Rails 4 application using devise/cancan for Authentication and Authorisation. I am using RSpec to write my tests. I have a (simplified for the cause of this post!) test that I'd like to…
ColinBruce
  • 49
  • 1
  • 2
  • 11
2
votes
0 answers

Webmock not catching external API calls

I need to mock an external API request as part of a new cucumber test. My usual practice is to first write the test that makes the call, and expect WebMock to complain about it. Then using the information that WebMock provides, stub the request. …
Daiku
  • 1,237
  • 11
  • 20
2
votes
2 answers

VCR with webmock blocking my rspec

I have a problem with my vcr with webmock spec. I have all the time the same problem when i run rspec spec/ .rvm/gems/ruby-2.1.3/gems/webmock-1.18.0/lib/webmock/http_lib_adapters/net_http.rb:114:in `request': Real HTTP connections are disabled.…
Panczo
  • 424
  • 5
  • 20
2
votes
1 answer

Remove VCR/Webmock logs from stdout when running tests

On a standard Rails 3.2.19 project, when running tests with RSpec I'm getting these logs on stdout when using VCR cassettes: Request[post]: https://svcs.sandbox.paypal.com/AdaptivePayments/Pay …
alf
  • 18,372
  • 10
  • 61
  • 92
2
votes
0 answers

How do I make webmock block/stub external requests from other gems?

I'm trying to use webmocks to test part of my app that interacts with an external service. However, all of the API calls are actually happening within another gem. So it continues to make the requests, even though I've enabled webmock. This properly…
will_in_wi
  • 2,623
  • 1
  • 16
  • 21
2
votes
1 answer

Webmock JSON response for geocoding

I try to stub a geocoding HTTP request with webmock. But I always get this error: NoMethodError: undefined method `[]' for nil:NilClass context "geocoding" do before :each do @user = Fabricate :user stub_request(:get,…
Jan
  • 12,992
  • 9
  • 53
  • 89
2
votes
1 answer

Webmock caching responses? Or: How to respond to repeated requests with randomized content

I've tried using a lambda in my custom response: stub_request( :post, 'http://blah.blah/token' ).to_return( status: 200, body: lambda { |a| '{"token":"' + SecureRandom.hex(20) + '","expires_in":"259200"}' } …
Ed Halferty
  • 136
  • 2
  • 10
2
votes
1 answer

Ruby TestUnit, VCR and HTTP API Requests

I am building an API wrapper and am writing some tests for it and I have a couple of questions. 1) How do I write an assert for calls where data doesn't exist? For example, looking up a member by id using the API but the user won't exist yet. 2) How…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
2
votes
1 answer

Test response of Rails 3 background job (API-Request) with VCR in Cucumber feature

I have a Rails 3 background job (delayed_job) which sends a hipchat / Campfire message to their API and I want to check the response in my Cucumber feature. Is there a way to get the last HTTP response(s) which VCR have recorded? The feature looks…
tbuehl
  • 178
  • 1
  • 6