testing

Testing tools, testing philosophy & methodology...
artemyarulin 2016-02-09T12:54:48.000077Z

Hi, what it the right approach to test async code with clojure? I’m not using core.async, I just have a small function which calls a callback at the end. Is there anything for that? Didn’t find anything in clojure.test

nberger 2016-02-09T13:07:35.000079Z

@artemyarulin: I'd say you just need to arrange your test fn in such a way so it waits for the async behavior to finish so you can do assertions about the result or about side effects. Or you could have assertions intermingled with your async code, that's fine too, again you just need to wait for that to execute

nberger 2016-02-09T13:08:03.000080Z

or do you think something else is needed?

artemyarulin 2016-02-09T13:09:15.000081Z

@nberger: Thank you. Hm, I know how can I block with core.async for example, but don’t want to bring it only for those thing. Assuming I have a simple function (fn[{:keys [a]} cb] (cb {:b a})) how can I test it?

nberger 2016-02-09T13:10:33.000082Z

hmm ok, I see... maybe you want a latch so you know when to finish?

2016-02-09T13:11:28.000083Z

@artemyarulin: you can make the callback deliver a promise and block the test dereferencing the promise, see https://clojuredocs.org/clojure.core/promise

👍 1
nberger 2016-02-09T13:11:30.000084Z

core.async tests use a latch, see an example in https://github.com/clojure/core.async/blob/master/src/test/cljs/cljs/core/async/tests.cljs#L17-L22

artemyarulin 2016-02-09T13:13:17.000086Z

not sure what latch means here, but @dialelo idea of using promise is a good one - no need to bring any dependency just for this small test. Thank you both in any case!

2016-02-09T13:14:27.000087Z

you're welcome!

2016-02-09T13:15:15.000088Z

there are a lot of goodies in core so usually I try to search there first

artemyarulin 2016-02-09T13:16:38.000089Z

yeap, I came from CLJS where we have https://github.com/clojure/clojurescript/wiki/Testing#async-testing, was looking for the same thing at first

nberger 2016-02-09T13:18:01.000091Z

I like the promise way too. The latch is not an additional dep, but I'm now seeing it's used in core.async cljs tests, not in clj tests. So yeah, promise is better 👍

artemyarulin 2016-02-09T13:18:24.000092Z

Thank you in any case :simple_smile:

🍻 2