Mock5
Mock5 allows to mock external APIs with simple Sinatra Rake apps.
Installation
Add this line to your application's Gemfile:
gem "mock5"
And then execute:
$ bundle
Or install it yourself as:
$ gem install mock5
Usage
SuccessfulRegistration = Mock5.mock("http://example.com") do
post "/users" do
# emulate successful registration
MultiJson.dump(
first_name: "Zapp",
last_name: "Brannigan",
email: "[email protected]"
)
end
end
UnsuccessfulRegistration = Mock5.mock("http://example.com") do
post "/users" do
halt 406, MultiJson.dump(
first_name: ["is too lame"],
email: ["is not unique"]
)
end
end
describe MyApi do
describe "successfull" do
around do |example|
Mock5.with_mounted SuccessfulRegistration do
example.call
end
end
it "allows user registration" do
expect{ MyApi.register_user }.not_to raise_error
end
end
describe "validation errors" do
around do |example|
Mock5.with_mounted UnsuccessfulRegistration do
example.call
end
end
it "returns errors" do
expect{ MyApi.register_user }.to raise_error(MyApi::ValidationError)
end
end
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request