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
?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With