Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. WebMock

WebMock

MITRubyv3.26.2

A Ruby library for stubbing and setting expectations on HTTP requests in tests.

GitHubGitHub
4.1k stars577 forks0 contributors

What is WebMock?

WebMock is a Ruby library for stubbing and setting expectations on HTTP requests in tests. It intercepts HTTP requests at the client library level, allowing developers to simulate external API responses, control network behavior, and verify that their code makes the expected HTTP calls without relying on live services.

Target Audience

Ruby developers writing tests for applications that make HTTP requests, especially those using RSpec, MiniTest, or Test::Unit and needing reliable, fast, and deterministic test suites.

Value Proposition

Developers choose WebMock for its deep integration with Ruby HTTP clients, flexible request matching, and seamless support for major testing frameworks, making it the go-to solution for mocking HTTP interactions in Ruby tests.

Overview

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

Use Cases

Best For

  • Testing Ruby applications that consume external APIs
  • Mocking HTTP requests in RSpec, MiniTest, or Test::Unit test suites
  • Simulating API failures, timeouts, or specific response codes
  • Verifying that your code makes the correct HTTP calls with expected parameters
  • Isolating tests from network dependencies for faster and more reliable test runs
  • Stubbing third-party service interactions in development or CI environments

Not Ideal For

  • Projects requiring real HTTP integrations with live servers for end-to-end testing
  • Applications using HTTP clients not listed in WebMock's supported libraries (e.g., newer or niche gems)
  • Testing scenarios involving HTTP/2, WebSockets, or other protocols beyond basic HTTP/1.1
  • High-performance test suites where the overhead of request interception is unacceptable

Pros & Cons

Pros

Extensive HTTP Library Coverage

Supports over 10 popular Ruby HTTP clients including Net::HTTP, HTTParty, and Excon, as listed in the README, ensuring broad compatibility without switching libraries.

Granular Request Matching

Matches requests by method, URI (with regex and RFC 6570 templates), headers, and body (JSON, XML, etc.), demonstrated in examples like stubbing with partial hashes or lambda blocks.

Seamless Test Framework Integration

Out-of-the-box support for RSpec, MiniTest, and Test::Unit with simple require statements, as shown in setup instructions for each framework.

Powerful Response Control

Allows custom responses, errors, timeouts, and Rack app delegation, with features like chained responses and dynamic evaluation from blocks or lambdas.

Cons

Net::HTTP Connection Quirks

The README warns that WebMock breaks Net::HTTP's default behavior by not connecting on start, requiring manual :net_http_connect_on_start configuration, which adds complexity.

Dependent on Supported Clients

Only works with the explicitly listed HTTP libraries; if a project uses an unsupported client, WebMock cannot intercept requests, forcing workarounds or alternative tools.

Stub Management Overhead

Managing stub precedence and clearing history requires careful setup (e.g., using WebMock.reset!), as the last declared stub wins and stale stubs can cause test pollution.

Frequently Asked Questions

Quick Stats

Stars4,052
Forks577
Contributors0
Open Issues155
Last commit1 month ago
CreatedSince 2009

Tags

#integration-testing#minitest#testing#rspec#ruby#http-client#http-mocking

Built With

R
Ruby

Included in

Ruby14.1k
Auto-fetched 1 day ago

Related Projects

fakerfaker

A library for generating fake data such as names, addresses, and phone numbers.

Stars11,598
Forks3,173
Last commit1 day ago
CapybaraCapybara

Acceptance test framework for web applications

Stars10,153
Forks1,469
Last commit5 days ago
factory_botfactory_bot

A library for setting up Ruby objects as test data.

Stars8,192
Forks2,568
Last commit7 days ago
VCRVCR

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

Stars6,045
Forks507
Last commit1 month ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub