简体   繁体   中英

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM