AlanAdams
AlanAdams

Reputation: 73

Is there any way to mock a function in clojure?

Is there any way to mock a function in clojure. For example I have this function: (defn a [x] (utils/count x)) I need to write a test, but I do not know how to mock the utils/count (for example). what if I have several functions inside the same function and I need to test it? (defn a [x] (utils/count x) (utils/count2 x) (test/other x))

Upvotes: 1

Views: 815

Answers (2)

Sean Corfield
Sean Corfield

Reputation: 6666

See also https://ask.clojure.org/index.php/9077/how-do-you-mock-in-clojure which suggests organizing your code so you don't need mocks, where possible.

Upvotes: 2

Lee
Lee

Reputation: 144226

You can use with-redefs e.g.

(deftest t
  (with-redefs [utils/count (fn [x] 2)
                utils/count2 (fn [x] 3)
                test/other (fn [x y] :result)]
    (let [result (a 2)])))

Upvotes: 2

Related Questions