Module: FulfilApi::TestHelper

Defined in:
lib/fulfil_api/test_helper.rb

Overview

The TestHelper module provides utility methods for stubbing HTTP requests to the Fulfil API in test environments. It uses WebMock to intercept and simulate API requests, allowing developers to test how their code interacts with the Fulfil API without making real HTTP requests.

This module is designed to be included in test cases where you need to simulate API interactions. It offers a flexible interface to stub requests for various models and resources, making it easier to write comprehensive and isolated tests.

Examples:

Including the TestHelper in your test case

class MyTest < Minitest::Test
  include FulfilApi::TestHelper

  def test_api_call
    stub_fulfil_request(:get, response: { name: "Product A" }, model: "product.product", id: "123")
    # Your test code here
  end
end

Instance Method Summary collapse

Instance Method Details

#stub_fulfil_request(method, response: {}, status: 200, **options) ⇒ WebMock::RequestStub

Stubs an HTTP request to the Fulfil API based on the provided parameters.

Examples:

Stub a GET request for a product model

stub_fulfil_request(:get, response: { name: "Product A" }, model: "product.product", id: "123")

Parameters:

  • method (String, Symbol)

    The HTTP method to be stubbed (e.g., :get, :post).

  • response (Hash) (defaults to: {})

    The response body to return as a JSON object (default is {}).

  • status (Integer) (defaults to: 200)

    The HTTP status code to return (default is 200).

  • options (Hash)

    Additional options, such as the model and ID for the request URL.

Options Hash (**options):

  • :model (String)

    The API model (e.g., ‘product.product’, ‘sale.sale’).

  • :id (String)

    The ID of the resource within the model (optional).

Returns:

  • (WebMock::RequestStub)

    The WebMock request stub object.



39
40
41
42
# File 'lib/fulfil_api/test_helper.rb', line 39

def stub_fulfil_request(method, response: {}, status: 200, **options)
  stubbed_request_for(method, **options)
    .and_return(status: status, body: response.to_json, headers: { "Content-Type": "application/json" })
end