Walkman v0.3.2 Walkman View Source

Walkman helps you isolate your tests from the outside world.

Getting started

# test/support/my_module_wrapper.ex
require Walkman
Walkman.def_stub(MyModuleWrapper, for: MyModule)
# config/config.exs
config :my_app, my_module: MyModule
# config/test.exs
config :my_app, my_module: MyModuleWrapper

Now you can replace MyModule with Application.get_env(:my_app, :my_module) everywhere in your application code.

# test/my_module_test.exs
test "MyModule" do
  Walkman.use_tape "MyModule1" do
    assert :ok = calls_my_module()
  end
end

Recording tapes

The first time you run the tests, Walkman will record test fixtures. You should commit these to your git repository. To re-record your fixtures, delete them and run the tests again. Every time you run your tests after this, Walkman will use the pre-recorded tapes.

If the implementation of a mocked module changes, any affected tests will automatically be re-recorded.

Integration mode

To disable stubs globally, use Walkman.set_mode(:integration). For example:

# test/test_helper.ex
if System.get_env("INTEGRATION_MODE"), do: Walkman.set_mode(:integration)

Concurrent tests

Concurrent tests are supported out of the box. When you use Walkman.use_tape/3, the tape is registered to the test process pid.

To access the tape from another process, call Walkman.share_tape/2.

Example:

test "can share tape with another process" do
  Walkman.use_tape "share_tape" do
    test_pid = self()

    spawn_link(fn ->
      Walkman.share_tape(test_pid)

      assert call_stub()
    end)
  end
end

If this is impractical, then making the tape global is another option. This can not be used with concurrent tests.

Example:

test "can access tape globally" do
  Walkman.use_tape("global tape", global: true) do
    spawn_link(fn ->
      assert call_stub()
    end)
  end
end

Link to this section Summary

Functions

Set Walkman's mode. The default is :normal

Share the tape with another process

Load a tape for use in a test

Link to this section Types

Link to this type

test_options() View Source
test_options() :: [preserve_order: boolean(), global: boolean()]

Link to this section Functions

Link to this macro

def_stub(wrapper_module, list) View Source (macro)

Link to this function

set_mode(mode) View Source
set_mode(mode :: :normal | :integration) :: :ok

Set Walkman's mode. The default is :normal.

Walkman has two modes:

  • :normal - if a tape is found, then the tape is replayed. If there is no tape, then a new one is made. This is the closest to how Ruby's VCR works.
  • :integration - calls are passed through to the implementation but no new tapes are made. Useful for running integration tests on the CI.
Link to this function

share_tape(test_pid, other_pid \\ self()) View Source

Share the tape with another process.

Example:

test "can access the stub from another process" do
  Walkman.use_tape("share tape") do
    test_pid = self()

    spawn_link(fn ->
      Walkman.share_tape(test_pid, self())
      assert call_mock()
    end)
  end
end
Link to this macro

use_tape(tape_id, test_options \\ [], list) View Source (macro)
use_tape(tape_id :: String.t(), test_options(), [{:do, term()}]) :: :ok

Load a tape for use in a test.

test "MyModule" do
  Walkman.use_tape "MyModule" do
    assert {:ok, _} = MyModule.my_function
  end
end