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
1
vote
1 answer

How do I allow the request with this path to pass the webmock?

I want to mock NetHTTP requests but some should be allowed. require "open-uri" require "webmock" WebMock.enable! When I declare an allowed request like this: WebMock.disable_net_connect! allow:…
Nakilon
  • 34,866
  • 14
  • 107
  • 142
1
vote
2 answers

Webmock and hash issues

The error message is this WebMock::Response::InvalidBody: must be one of: [Proc, IO, Pathname, String, Array]. 'Hash' given I am using the code below to test the google library for getting the user info in my controller stub_request(:get,…
Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82
1
vote
1 answer

wrong number of arguments in Net::HTTP request

In a Sinatra application, I'm trying to make my own wrapper around Net::HTTP, but I'm stuck at one of my tests. I'm using Webmock to stub a request and pass it to the request method of Net::HTTP. I do the testing with RSpec. The error I'm getting…
Xirux Nefer
  • 830
  • 1
  • 7
  • 19
1
vote
1 answer

Using RSpec/webmock to stub requests for various status codes

I need to stub requests to an external API with webmock/webmock but I need to test for a handful of responses (200, 404, 503, etc.). What's the best way to do this cleanly? My first hacky thought was that you can set something unique in the headers,…
RangerRanger
  • 2,455
  • 2
  • 16
  • 36
1
vote
1 answer

FactoryGirl used with webmock

My factory has an url field(used to fetch youtube embedded video): factory :commercial do url 'https://www.youtube.com/watch?v=BTTygyxuGj8' end In my tests I tried to mock the request to youtube. I added following to my spec_helper: require…
sonalkr132
  • 967
  • 1
  • 9
  • 25
1
vote
1 answer

Can't stub request to Flickr API

I can't stub request to Flickr API for my controller test. I use gem 'flickraw' for getting data from Flickr API. flickr_search_controller.rb: module Dashboard class FlickrSearchController < Dashboard::BaseController respond_to :js def…
1
vote
1 answer

Does a custom matcher or match_requests_on override playback repeats?

I have an integration test for some code that sends SMS messages. The test sends the initial message, say, "Hello, world", and uses VCR to validate the call to the external SMS provider was made via an HTTP API. The test then simulates sending a…
Martin Streicher
  • 1,983
  • 1
  • 18
  • 18
1
vote
2 answers

Stubbing multiple requests to the same uri

I would like to use the same uri with different request params, how can I do this? The following example seems does not work ( stub_request(:post, "http://tests/1/users/create"). with({email: 'wrong_format'}). to_return(status: 400,…
Aydar Omurbekov
  • 2,047
  • 4
  • 27
  • 53
1
vote
1 answer

Is it possible to use WebMock with Rack Test?

Using WebMock to stub an Oauth 2 Provider. The issue is that I want to use Rack Test. Rack Test only runs against an instance of the app, and does not know about external HTTP hosts like the Oauth 2 Provider. WebMock.stub_request(:get,…
B Seven
  • 44,484
  • 66
  • 240
  • 385
1
vote
0 answers

WebMock: undefined method `gsub!' for nil:NilClass after using `stub_request`

My app is using Carrierwave + Fog for storing images on Amazon S3. I have an Rspec feature test for the registration process that uses Capybara's attach_file helper to add the file. The spec looks like this: feature 'Registration' do before do …
sixty4bit
  • 7,422
  • 7
  • 33
  • 57
1
vote
1 answer

VCR + Webmock + net_http + NTLM not working without monkey patch

In order to get NTLM working with Webmock + net_http, I need the net_http_connect_on_start option enabled. For example, in the console, or the beginning of a spec, I can use: WebMock.allow_net_connect! net_http_connect_on_start: true but VCR…
pduey
  • 3,706
  • 2
  • 23
  • 31
1
vote
1 answer

How to pass data along with a ruby block?

I'm trying to pass some data as a block to some external API. It would be a hassle to accommodate it to accepting additional parameters. If it were javascript, I might make it like so: var callback = function() { // do…
x-yuri
  • 16,722
  • 15
  • 114
  • 161
1
vote
0 answers

How to test delays between requests?

First, the big picture. I'm writing a sinatra-based web app. One thing it's going to do is determining users' birth years. For that it interacts with the other app. It first asks the user's data, like first name, last name, sex, birthday (month and…
x-yuri
  • 16,722
  • 15
  • 114
  • 161
1
vote
0 answers

Unable to stub external requests for fake braintree

I have a controller test in which I'm testing a method that also tries to process a payment with braintree. I tried adding fake braintree to the project, but when I run the test I'm told by webmock that I need to stub this request: Real HTTP…
xxyyxx
  • 2,306
  • 3
  • 24
  • 34
1
vote
2 answers

Rails: How to stub and test this external service call?

Hello I have something like the following code inside a rails model: class Page < ActiveRecord::Base def calculate_page_speed(url) browser = Watir::Browser.new :phantomjs start = Time.now browser.goto url finish = Time.now …
JohnDel
  • 2,092
  • 8
  • 35
  • 64