Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

analog of `let` in ExUnit

As you know, rspec supports let in its tests. It's quite helpful and shorts code a lot by predefining common variables and share them between tests.

How can I invoke this functionality in ExUnit?

like image 764
asiniy Avatar asked Apr 22 '16 06:04

asiniy


2 Answers

I think the better way to have the same behavior on Elixir with ExUnit, is using setup_all or setup.

The setup_all callbacks are invoked once to setup the test case before any test is run and all setup callbacks are run before each test. No callback runs if the test case has no tests or all tests were filtered out.

Complete documentation can be found here: https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html

like image 162
WeezHard Avatar answered Oct 15 '22 18:10

WeezHard


I think the closest analog is returning a context from a setup hook. Take a look at this example adapted from the ExUnit.Callbacks documentation:

defmodule AssertionTest do
  use ExUnit.Case, async: true

  setup do
    {:ok, hello: "world"}
  end

  test "a test", context do
    assert(context[:hello] == "world")
  end
end
like image 7
Paweł Obrok Avatar answered Oct 15 '22 16:10

Paweł Obrok