Api clients / Ruby / V1 / Guides

Mock API Calls in the Ruby API Client

Webmock

For testing purposes, you may want to mock Algolia’s API calls. We provide a WebMock configuration that you can use including algolia/webmock.

Add gem webmock to your Gemfile to use the following configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require 'algolia/webmock'

describe 'With a mocked client' do

  before(:each) do
    WebMock.enable!
  end

  it "shouldn't perform any API calls here" do
    index = client.init_index('friends')
    index.add_object!({ name: 'John Doe', email: 'john@doe.org' })
    index.search('').should == { hits: [{ objectID: 42 }], page: 1, hitsPerPage: 1 } # mocked
    index.clear_index
    index.delete_index
  end

  after(:each) do
    WebMock.disable!
  end

end

Did you find this page helpful?