ExUnited v0.1.2 ExUnited.Case View Source

This module makes it possible to seemlessly execute assertions and refutations within spawned nodes.

You can setup your test module by either using ExUnited.Case instead of ExUnit.Case:

defmodule MyNodesTest do
  use ExUnited.Case
end

Or by importing the ExUnited.Case module:

defmodule MyNodesTest do
  use ExUnit.Case
  import ExUnited.Case
end

Writing assertions and refutations within the context of a certain spawned is pretty straight forward with the use of the ExUnited.Case.as_node/2 function:

Example

defmodule MyNodesTest do
  use ExUnited.Case

  setup do
    {:ok, spawned} = ExUnited.spawn([:bruce, :clark])

    on_exit(fn ->
      ExUnited.teardown()
    end)

    spawned
  end

  test "assertions and refutations within node contexts", spawned do
    bruce = get_in(spawned, [:bruce, :node])

    as_node(bruce) do
      assert :"bruce@127.0.0.1" = Node.self()
      refute :"clark@127.0.0.1" == Node.self()
    end

    as_node(:clark) do
      assert :"clark@127.0.0.1" = Node.self()
      refute :"bruce@127.0.0.1" == Node.self()
    end
  end
end

See ExUnited.Case.as_node/2 for more information.

Link to this section Summary

Functions

Injects use ExUnit.Case and import ExUnited.Case, except: [send_error: 2] in the test module.

A convenience macro function for writing assertions and refutations from within spawned nodes.

Link to this section Functions

Link to this macro

__using__(opts)

View Source (macro)

Injects use ExUnit.Case and import ExUnited.Case, except: [send_error: 2] in the test module.

Gets triggered after having put use ExUnited.Case in your test module.

Link to this macro

as_node(node, binding \\ [], list)

View Source (macro)

A convenience macro function for writing assertions and refutations from within spawned nodes.

It makes writing tests so much easier and more readable. The function accepts the following:

  • node - the simple name (used during spawning) or the node name
  • binding - a keyword list containing variables which will be available in the code block (optional)
  • block - the code block containing assertions and/or refutations

Example

test "assertions and refutations within the right context", spawned do
  clark = get_in(spawned, [:clark, :node])
  bruce = get_in(spawned, [:bruce, :node])

  assert :"captain@127.0.0.1" == Node.self()
  assert [:"bruce@127.0.0.1", :"clark@127.0.0.1"] = Node.list() |> Enum.sort()

  as_node(clark) do
    nodes = Node.list()
    refute :"captain@127.0.0.1" == Node.self()
    assert :"clark@127.0.0.1" = Node.self()
    assert [:"captain@127.0.0.1"] = Node.list()
    assert ^nodes = Node.list()
  end

  as_node(:bruce, spawned_node: bruce) do
    assert :"bruce@127.0.0.1" = Node.self()
    assert :"bruce@127.0.0.1" = spawned_node
    assert ^spawned_node = Node.self()
    assert ^spawned_node = :"bruce@127.0.0.1"
    assert [:"captain@127.0.0.1"] = Node.list()
  end
end