I am trying to mock UUID generation for a test case with with-redefs
. It works as long as I call my function directly, but it stops working as soon as I map my function over some data.
I created a very small example that shows my problem here:
https://replit.com/@JacobEmcken/LovingGorgeousAfkgaming
I don't understand why map
makes a difference nor how I should mock my id generation if this doesn't work 😕
Does anyone have an idea?
map
is lazy, so the redef doesn’t apply when the return value is realized
user=> (with-redefs [uuid (uuid-mock)] (mapv geif-ids '(1 2)))
[("UUID-1") ("UUID-2" "UUID-3")]
using mapv
(or (into [] ...
or doall
will realize the whole list at the time it’s called instead of when it’s printed to the console or used in some calculation
user=> (with-redefs [uuid (uuid-mock)] (map geif-ids '(1 2)))
(("a52c1525-63ed-46c1-8070-5b27d193f2ce") ("4ce9d4e8-ff19-4947-8f7f-fa20d1b47ad9" "47727d0c-c222-4992-846b-6e4b93cffb83"))
user=> (with-redefs [uuid (uuid-mock)] (mapv geif-ids '(1 2)))
[("UUID-1") ("UUID-2" "UUID-3")]
user=> (with-redefs [uuid (uuid-mock)] (doall (map geif-ids '(1 2))))
(("UUID-1") ("UUID-2" "UUID-3"))
user=> (with-redefs [uuid (uuid-mock)] (into [] (map geif-ids '(1 2))))
[("UUID-1") ("UUID-2" "UUID-3")]
ahh that makes sense. Thanks a lot :thumbsup: