testing

Testing tools, testing philosophy & methodology...
Jacob Emcken 2021-06-09T13:25:00.030100Z

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?

NoahTheDuke 2021-06-09T13:38:12.031Z

map is lazy, so the redef doesn’t apply when the return value is realized

1❤️
NoahTheDuke 2021-06-09T13:38:20.031200Z

user=> (with-redefs [uuid (uuid-mock)] (mapv geif-ids '(1 2)))
[("UUID-1") ("UUID-2" "UUID-3")]

NoahTheDuke 2021-06-09T13:39:05.032100Z

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

NoahTheDuke 2021-06-09T13:40:05.032600Z

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")]

Jacob Emcken 2021-06-09T14:05:03.034100Z

ahh that makes sense. Thanks a lot :thumbsup: