百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mock5

> 编程语言
开源

使用 Sinatra 创建和管理 API 模拟

297 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

使用 Sinatra 创建和管理 API 模拟

Mock5

Mock5 allows to mock external APIs with simple Sinatra Rack apps.

Installation

This gem could be useful for testing, and maybe development purposes. Add it to the relevant groups in your Gemfile.

ruby
gem "mock5", groups: [:test, :development]

and run bundle.

Usage

mock

Use this method to describe API you're trying to mock.

ruby
weather_api = Mock5.mock("http://weather-api.com") do
  get "/weather.json" do
    MultiJson.dump(
      location: "Philadelphia, PA",
      temperature: "60F",
      description: "Sunny"
    )
  end
end

mount

Use this method to enable API mocks you've defined previously.

ruby
Mock5.mount weather_api, some_other_api
Net::HTTP.get("weather-api.com", "/weather.json") # => "{\"location\":...

unmount

Unmounts passed APIs if thery were previously mounted

ruby
Mock5.unmount some_other_api # [, and_another_api... ]

mounted_apis

This method returns a Set of all currently mounted APIs

ruby
Mock5.mounted_apis # => { weather_api }
Mock5.mount another_api
Mock5.mounted_apis # => { weather_api, another_api }

with_mounted

Executes the block with all given APIs mounted, and then unmounts them.

ruby
Mock5.mounted_apis # => { other_api }
Mock5.with_mounted weather_api, other_api do
  Mock5.mounted_apis # => { other_api, weather_api }
  run_weather_api_test_suite!
end
Mock5.mounted_apis # => { other_api }

Example

Say you're writing a nice wrapper around remote user management REST API. You want your library to handle any unexpected situation aproppriately and show a relevant error message, or schedule a retry some time later.

Obviously, you can't rely on a production API to test all these codepaths. You probably want a way to emulate all these situations locally. Enter Mock5:

…

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言