PactEx.MockServer (pact_ex v0.1.0)

Copy Markdown View Source

A running Pact mock server.

Start one with PactEx.Consumer.start_mock_server/3, or let PactEx.Consumer.run/4 manage it. Send the requests of your test to url.

Summary

Functions

Returns true if every expected request arrived and no unexpected request did.

Returns the mismatches seen so far, as decoded maps.

Stops the server and frees its resources. Always returns :ok, also when the server was already stopped.

Writes the pact file to the server's pact_dir, creating the directory if needed.

Types

t()

@type t() :: %PactEx.MockServer{
  pact_dir: Path.t(),
  pact_ref: reference(),
  port: pos_integer(),
  url: String.t()
}

Functions

matched?(mock_server)

@spec matched?(t()) :: boolean()

Returns true if every expected request arrived and no unexpected request did.

Returns false for a server that was already stopped.

mismatches(mock_server)

@spec mismatches(t()) :: [map()]

Returns the mismatches seen so far, as decoded maps.

Returns [] when there are none, and for a server that was already stopped. See PactEx.MismatchError for the "type" values.

stop(mock_server)

@spec stop(t()) :: :ok

Stops the server and frees its resources. Always returns :ok, also when the server was already stopped.

Read matched?/1 and mismatches/1 before calling it.

write_pact(mock_server, opts \\ [])

@spec write_pact(t(), keyword()) :: :ok | {:error, PactEx.Error.t()}

Writes the pact file to the server's pact_dir, creating the directory if needed.

The file is named <consumer>-<provider>.json.

Options

  • :overwrite - when false (the default), merges with an existing file. When true, replaces it.

Returns {:error, %PactEx.Error{}} if the file cannot be written, or if the server was already stopped (code 3). Raises ArgumentError on unknown options or a non-boolean :overwrite.