Loopback (Loopback v0.1.0)

Copy Markdown View Source

Run your tests against fake external services.

See the README for how to set up Loopback, how it works, and why fakes change the way you write tests.

Summary

Functions

Allows allowed to use the fakes of owner, usually the test process.

Starts the fakes for the test that calls it.

Returns the PID of the running fake implemented by module.

Functions

allow(owner, allowed)

@spec allow(pid(), pid() | (-> pid() | [pid()])) :: :ok

Allows allowed to use the fakes of owner, usually the test process.

Tests call it to let processes that they didn't start, usually workers in your app's supervision tree, use their fakes:

Loopback.allow(self(), Process.whereis(MyApp.Worker))

allowed can also be a function returning a PID or a list of PIDs. It's called when a request doesn't find any fakes otherwise, so it can allow processes that don't exist yet:

Loopback.allow(self(), fn -> Process.whereis(MyApp.Worker) end)

owner can also be a process that is allowed itself. With shared: true, every process already uses the test's fakes, so it does nothing.

Raises ArgumentError if owner neither started fakes nor is allowed to use them.

start_fakes!(fakes, opts \\ [])

@spec start_fakes!(
  [Supervisor.child_spec() | {module(), term()} | module()],
  keyword()
) :: :ok

Starts the fakes for the test that calls it.

setup tags do
  Loopback.start_fakes!([MyApp.SomeApiFake], shared: not tags[:async])
end

fakes is a list of child specifications. A request goes to the first fake in the list that matches it. They run under a supervisor started with ExUnit.Callbacks.start_link_supervised!/2, so they stop when the test ends, and a crash of a fake fails the test. Call it from the test process, in a setup callback or in the test. It returns :ok, so it can be the whole body of a setup block.

See How Loopback works for the requests that reach the test's fakes.

Options

  • :shared - whether every process uses the test's fakes. Only for tests that aren't async. Defaults to false.
  • :unmatched - what to do with a request that no fake matches, neither the test's nor a global one: :raise, or :passthrough, which sends it to the real service. Defaults to :raise.

whereis!(module)

@spec whereis!(module()) :: pid()

Returns the PID of the running fake implemented by module.

Helpers in fakes call it to find the fake's PID. Fakes get a generated whereis!/0 that calls it with their module:

def trigger_downtime do
  GenServer.call(whereis!(), :downtime)
end

Raises if no fakes were started for the calling process, or if this fake isn't running.