Closed schaary closed 7 years ago
All struct errors happen at compile time so indeed they won't be caught at runtime. To put it in other words, the module FooTest
does not successfully compile.
Hi @josevalim, I was wondering if you have opinions about using something like "initializer methods" for this?
I was test driving a similar bit of code and added a new/2
method as a documented way (I think I added a doctest instead in my case) to create a valid %Foo{}.
defmodule FooTest do
use ExUnit.Case
test "new/2 creates a valid Foo" do
%Foo{} = Foo.new(1, "name")
end
end
defmodule Foo do
@enforce_keys [:id, :name]
defstruct :id, :name, :foo, :bar
def new(id, name) do
%Foo{id: id, name: name}
end
end
I was also considering doing something like https://thepugautomatic.com/2015/09/testing-compile-time-exceptions-in-elixir/ to test the enforce_keys
explicitly as well, but that seems like overkill in this case?
Environment
Elixir 1.5.1
The test looks like this:
Expected behavior
I would expect the test runs successfully. When I try the example from https://hexdocs.pm/ex_unit/ExUnit.Assertions.html#assert_raise/2 the test runs successfully. But with
@enforce_keys
and a test ofArgumentError
not.