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

Webmock to return an Response Object from an API?

I call out to an API, where I then take their xml and parse it to return a Response object. Typical stuff for calling a 3rd party API. So now in my tests I want to use webmock to say "When you get a request to go to example.com, just give me a…
pjammer
  • 9,489
  • 5
  • 46
  • 56
1
vote
2 answers

expect(WebMock).to have_requested(:get, "any url")

I'm am adding Webmock stubbing to my tests. Right now, I'm testing that a request was made, but I'd like to do so without matching the full url with all its parameters. expect(WebMock).to have_requested(:get,…
Jeff Zivkovic
  • 547
  • 1
  • 4
  • 20
1
vote
0 answers

WebMock stopping selenium from deleting session

System Ruby v2.6 WebMock gem v3.18.1 Selenium Webdriver gem v4.1.0 RSpec gem v3.11.0 Capybara gem v3.36.0 Summary Whilst successfully running a suite of tests on GitLab CI[1], WebMock intercepts a request from Capybara's Selenium driver to delete…
James
  • 255
  • 2
  • 11
1
vote
1 answer

WebMock stub request with dynamic body in response

I am attempting to stub a request with WebMock and have the body of the response expect any value within a given regex; something to the tune of: stub_request(:get, "someurl"). with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). …
Tony Beninate
  • 1,926
  • 1
  • 24
  • 44
1
vote
4 answers

Stub Httparty call: Wrong number of arguments (given 2, expected 1)

I created a simple ruby file (not Rails) and I am trying to test (using Rspec) a method where I am calling an API. In the test I am trying to mock the call via WebMock but it keeps giving me this error: Requests::FilesManager#display fetches the…
josegp
  • 499
  • 6
  • 21
1
vote
0 answers

How to stub HTTP requests on Common LISP tests?

Is there any tool usually used in Common LISP tests to block all network requests and stub responses for specific URLs? Just for reference, in Ruby we usually use: https://github.com/bblimke/webmock https://github.com/chrisk/fakeweb (or even more…
1
vote
0 answers

RSpec tests not loading Stripe card element - works correctly live/locally

I think this is down to my lack of experience with RSpec but its driving me crazy - the stripe card element isn't generating on the screen so the test fails with a js error due to Stripe not working/having the actual fields available. I've tracked…
Becky Green
  • 625
  • 5
  • 21
1
vote
0 answers

WebMock stub_request with hash_including -- matching only part of json

I wold like to test whether a request contained some inner value in json. Let assume the request body is: level1: { level2a: "value1", level2b: "value2", ... }, ... } Then I have the following code: …
paliwodar
  • 81
  • 6
1
vote
1 answer

Rails WebMock error on the second request try ::NetConnectNotAllowedError

I want to mock custom devise strategies to authenticate user in my feature specs. To stub request to 3th party app I'm using WebMock with the implementation below: spec/utility/stub_methods.rb def stub_aware_auth(creds, returns_token, _valid) …
mr_muscle
  • 2,536
  • 18
  • 61
1
vote
2 answers

How can I trap errors correctly in a Sidekiq worker when testing with RSpec?

I have a relatively simple worker that uses Excon to grab something from the internet. I'm trying to be a good tester and use Webmock to force stubbing of the internet interactions, so that I'm actually testing what the code should do based on…
Erik Jacobs
  • 841
  • 3
  • 7
  • 19
1
vote
1 answer

Stub Faraday request with Webmock - help needed

I need help stubbing a request using Faraday gem. I am making this request URL='https://secure.snd.payu.com//pl/standard/user/oauth/authorize'.freeze url_encoded = 'grant_type=client_credentials' \ + "&client_id=#{ENV['client_id'}" \ +…
jedi
  • 2,003
  • 5
  • 28
  • 66
1
vote
0 answers

Rspec mocks being ignored in before block when using js: true?

I'm testing a Rails application using RSpec/Capybara and Webmock. I'm setting js:true for a particular suite of tests, whilst stubbing out a web request using Webmock in a before block. One of the tests fails intermittently - the error returned is…
1
vote
0 answers

dynamic parameters for post method in webmock

I'm trying to mock some requests which have dynamic parameters and they constantly change each time I ran the tests! Is there any way I could set parameters with variables? for Get method you can set template but nothing for Post! body = { …
mehrnoush
  • 11
  • 3
1
vote
0 answers

response.body is "" even though webmock .to_return(:body => "return") is set

I'm writing unit tests for a Rails service using minitest. A snippet of my test looks like this: stub_request(:get, @service_url) .with(:headers => @authheader) .to_return(:body => "ABC", :status => 200) get :index After the get…
Jason B
  • 11
  • 2
1
vote
1 answer

Stub Request is not working

This webmock stub request is not working while running the test. Here is the controller. class AdvisorDashboardController < ApplicationController load_and_authorize_resource :class => AdvisorDashboardController require 'Numeric.rb' include…
Ganesh Kumar
  • 1,631
  • 2
  • 21
  • 35