Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to stub(mock?) Ecto.UUID.generate in an ExUnit test?

I'm using Ecto.UUID.generate to create a random token on a user model.

In my ExUnit test, I want to test the controller that calls the creation route, however because the token is always random, I can't deterministically test the result.

Ideally, I want to be able to stub the Ecto.UUID.generate in my test so it always creates the same UUID so I can test the response correctly.

Thanks :)

like image 206
TheStoneFox Avatar asked May 07 '15 06:05

TheStoneFox


1 Answers

There are mocking solutions in Elixir and Erlang but they are not used a lot. If you are further interested, here is an article: http://blog.29steps.co.uk/post/105715556278/testing-api-web-calls-in-elixir-using-meck

Honestly, I would just avoid the mock. If you have an API, test the returned ID is an UUID (you can see if Ecto.UUID.dump(uuid) returns {:ok, something}). If it is a browser/html app, it is likely you care about the UUID even less.

like image 156
José Valim Avatar answered Nov 02 '22 03:11

José Valim