Mock5

Gem Version Build Status Code Climate

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

  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