On this page
Overview
WebMock is a Ruby testing library for stubbing HTTP requests and verifying request expectations. It intercepts requests at a low HTTP-client layer, letting tests use a common stubbing model across Net::HTTP and several other Ruby HTTP libraries.
Features and best fit
Based on official documentation; not hands-on tested · Content checked:
Key features
Stub HTTP responses without connecting to the real network
Tests can define response bodies, statuses, and headers for matching requests and receive deterministic results from application HTTP calls.
Sources: [1]
Match requests by method, URI, headers, body, and more
Request matching can combine methods, URIs, query parameters, headers, bodies, regular expressions, and related conditions.
Sources: [1]
Integrate with RSpec, Minitest, Test::Unit, and multiple HTTP clients
Framework-specific require entrypoints simplify test-suite integration, while adapters cover many commonly used Ruby HTTP client libraries.
Sources: [1]
Best fit
Fits fast and reproducible unit tests around external HTTP integrations
It isolates tests from network latency, rate limits, and service outages while verifying how application code constructs requests and handles responses.
Sources: [1]
Before adoption
Use another test layer to catch drift from real service contracts
Mocked responses cannot detect provider-side schema, status-code, or authentication changes by themselves. Contract or integration tests should verify compatibility with real services separately.
Sources: [1]
Configure network blocking and localhost allowances intentionally
WebMock.disable_net_connect! can prohibit real external requests, with options such as allowing localhost. Make the policy explicit so accidental network calls do not leak into unit tests.
Sources: [1]
Official sources
- [1]WebMock v3.26.4 README(2026-10-03)
- [2]WebMock v3.26.4 release(2026-10-03)
- [3]WebMock MIT license(2026-10-03)
Supplemental curator note
WebMock makes external-API unit tests fast and deterministic, but stubs can drift away from real service contracts. Keep contract or integration tests in a separate layer rather than treating mocked behavior as proof that the external integration still works.
Try it in 3 steps
- 1
Install WebMock 3.26.4
Install the pinned stable release into a Ruby environment.
gem install webmock -v 3.26.4 - 2
Create a minimal stub-only script
Create a script that replaces the example.com GET request with a stub response and does not call the real network.
printf '%s\n' 'require "uri"' 'require "net/http"' 'require "webmock"' 'include WebMock::API' 'WebMock.enable!' 'stub_request(:get, "http://example.com/").to_return(body: "stubbed")' 'puts Net::HTTP.get(URI("http://example.com/"))' > webmock_demo.rb - 3
Verify the stubbed response
If it prints stubbed, request interception and response stubbing are working.
ruby webmock_demo.rb
Growth
Growth trends · Last 30 days
4,049 Stars
Trend data is still being collected.
Development activity
Last 90 days · weekly
- Commits (last 30 days)
- 0
- Open PRs
- 27
Development activity is still being collected.
Built with
Categories and tags
Categories
GitHub data
GitHub dataView detailed GitHub data
Related information
Write a related articleShare a guide or use case for this OSS in Markdown. Articles are published after administrator approval.
Explore next
- Cypress51,043 Stars
1 shared tag(s) · 2 shared category(s)
run browser end-to-end and component tests through an interactive runner and CLI
TypeScript - Appium22,040 Stars
1 shared tag(s) · 2 shared category(s)
automate end-to-end tests for Android, iOS, and desktop apps through WebDriver-compatible APIs
TypeScript - jsdom21,692 Stars
1 shared tag(s) · 2 shared category(s)
emulate a subset of DOM and HTML standards in Node.js for testing and scraping
JavaScript - Mockito15,454 Stars
1 shared tag(s) · 2 shared category(s)
replace Java collaborators and keep unit tests focused
Java - BenchmarkDotNet11,506 Stars
1 shared tag(s) · 2 shared category(s)
turn .NET methods into reproducible benchmarks with automated warmup, statistics, diagnosers, and result exporters
C# - RSpec117 Stars
2 shared tag(s) · 2 shared category(s) · Same language
describe Ruby behavior with examples, expectations, and test doubles and run it through a CLI
Ruby
Report incorrect information
Tell us if any listing information is incorrect or outdated.