Group side effect checks in Clojure

I have a function that retrieves messages from the 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 side effects, one depends on network access, the other on the IO drive on the local machine.

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

+4
source share
1 answer

Using core.test, I usually select the grinding functions withwith-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))))))

mq, logit, , your-test-trigger - , get-message.

+6

Source: https://habr.com/ru/post/1529016/


All Articles