Testing a function with guards

Im still trying to adapt to the dynamically typed nature of elixir and I have a doubt about testing a function that has a guard

I have

def new(id) where is_integer(id)

so how do I write a test for it?
I can test it when id is a integer pretty easy,
but I would like to write a test when id is not an integer

Im asking because I can call new("hello") in the code and mix compile doesn’t cry at all
so Im guessing this will blowup in runtime.

when I pass a string in test I get

** (FunctionClauseError) no function clause matching in Dealer.new/1

and test fails (expected) and shows a stacktrace

What is the common practice here?

since Im still struggling with “when to use guards” any tips are appreciated as well


Well, assert_raise exists.

1 Like

Usually you do not test calling with a string when the contract is “ints only”.

Though if you really want to, you can use assert_raise/2,3.


If You want to get warnings when your code use wrong type, You would need a tool like dialyxir :slight_smile: