mario-mazo

mario-mazo

Testing a function with guards

Hello
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

thanks

Marked As Solved

NobbZ

NobbZ

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.

Also Liked

kokolegorille

kokolegorille

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

dimitarvp

dimitarvp

Well, assert_raise exists.

Last Post!

kokolegorille

kokolegorille

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

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement