简体   繁体   中英

Doctesting a function that returns different map during every run

Let's say that my function returns a map and some of the value may be randomly generated. I'd like to be able to at least test again output type, or in another words - to check from the doctest level weather returned value is a map. Eventually does it contains specific keys. Is it even possible? The function call may look following:

iex> MyApp.function(params, opts)
%{_}

The output can't be a pattern, but you can either use is_map or the match? macro with the pattern %{} , both of which will return true if the value is a map.

iex> is_map MyApp.function(params, opts)
true
iex> match? %{}, MyApp.function(params, opts)
true

While answer by @Dogbert is perfectly correct, it could not be used in all cases. When one needs to check a value that is unpredictable in advance (say, randomly generated,) there is still an ability to do that with ExUnit .

Each run of test suite prints out the Random seed value as the very last line of test run:

Randomized with seed 486290

It might be recorded and passed back to ExUnit.configure/1 . In such a case, random value returned from the function will be the same (it will not change between different runs.)

This trick won't work for data, received from third-party services, of course.

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