Question

I have a function that pulls messages from an AMPQ message bus:

(defn get-message [queue client]
    (let [msg (mq/get-message client queue)]
    (if msg
        (logit msg))))

mq/get-message and logit are both side-effects, one depends on network access, the other on disk IO on the local machine.

Is there an idiomatic way of unit-testing side-effects in Clojure? My first thought was mocks/stubs, but if there was something better.

Was it helpful?

Solution

Using core.test I usually go the way of mocking up the side effectish functions using with-redefs

(deftest ampq-messaing
  "Test messaging"
  (let [logit-msg (atom nil)]
    (with-redefs [mq/get-message (fn [] "message")
                  logit (fn [msg] 
                           (reset! logit-msg msg))]
      (let [response (your-test-trigger)]
        (is (= "message" @logit-msg))))))

In that case I'm testing the returned message from mq is the one used in logit, and I'm assuming that your-test-trigger is something that triggers a call to get-message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top