ActiveresourceHttpmockRecord

Testing models which communicate with remote web services could be painful. This gem provides a simple way to record HTTP request and response through ActiveResource, and playback with ActiveResource::HttpMock.

Features

If your application uses ActiveResource:

  • ActiveresourceHttpmockRecord works together with your test code, records HTTP requests generated by an ActiveResource model and HTTP response coming from the remote web service.
    • They're stored as YAML files to play back later
  • During the same test code runs after that, ActiveresourceHttpmockRecord mocks HTTP requests and responses recorded istead of creating new actual HTTP sessions with ActiveResource.

Supported testing tool

  • RSpec

Installation

Add this line to your application's Gemfile:

gem 'activeresource_httpmock_record'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activeresource_httpmock_record

Usage

RSpec

You may write your spec like:

describe User do
  subject(:user) { User.find(1) }

  it 'has a name' do
    expect(user.name).to be_present
    expect(user.name).to be_a(String)
  end
end

When running it, User.find(1) causes a HTTP request.

To activate ActiveresourceHttpmockRecord, add a metadata hash httpmock: <file_name> to groups or examples in your spec.

describe User do
  subject(:user) { User.find(1) }

  it 'has a name', httpmock: a_user do
    expect(user.name).to be_present
    expect(user.name).to be_a(String)
  end
end

Once you run the spec, ActiveresourceHttpmockRecord record HTTP request and response as a a_user.yml, and if you run it again, ActiveresourceHttpmockRecord load the HTTP session from file_name.yml and mock it for you.

Warning

ActiveresourceHttpmockRecord doesn't support a multi-threaded testing like Capybara with js: true option.

Configuration

You can change the mock file store by adding the following lines (typically spec_helper.rb for RSpec):

ActiveResourceHttpMock.configure do |config|
  config.httpmock_path = '/path/to/directory'
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