When writing tests I frequently write small helpers which are used to
shorten the tests.
Examples:
- when testing mixins, inside describe I added a module with use Mixin,
some_configuration and created helper that called mixin functions
- when testing Phoenix controller I created function that requests correct
path, given conn as an argument
I noticed that describe don't create module scope so while it looks like
those functions are scoped to given describe, they're shared between all
tests in the file.
If implemented, this feature could improve locality of helpers (so you
don't have to scroll all the way down to see helper's implementation) and
allowed helpers to share the names.
Example:
defmodule DescribeFns do
def foo, do: :foo
def bar, do: 5
end
defmodule DescribeFnsTest do
use ExUnit.Case
describe "foo/0" do
test "foo test" do
assert "foo" == stringify(DescribeFns.foo())
end
defp stringify(atom) do
Atom.to_string(atom)
end
end
describe "bar/0" do
test "bar test" do
assert "5" == stringify(DescribeFns.bar())
end
defp stringify(number) do
Integer.to_string(number)
end
end
end
Compiles with warning:
*$ mix test*
warning: this clause for stringify/1 cannot match because a previous clause
at line 15 always matches
test/describe_fns_test.exs:25
And fails on bar/0 test.
Elixir forum question
<https://elixirforum.com/t/how-are-functions-scoped-within-exunit-describe-blocks/37655>
about that behavior
I'm posting twice, previous post was immediately deleted for some reason.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/b5ddc4fb-d671-4630-843e-0acb3a721acbn%40googlegroups.com.