如何使用with-redefs模拟对同一函数的多个调用?

Fre*_*001 2 unit-testing clojure mocking compojure

我想能够模拟MyFunction然而我需要模拟在MyFunction调用时返回不同的值.

是否可以with-redefs根据函数的调用顺序返回不同的值?

(testing "POST /foo/bar and return ok"
  (with-redefs [->Baz (fn [_]
                    (reify MyProtocol (MyFunction [_] [{:something 1}]))
                    (reify MyProtocol (MyFunction [_] [{:something 2}])))]

    (let [response (routes/foo {:request-method :post
                            :uri            "/foo/bar"
                            :query-params   {}
                            })]

      (is (= (:status response) 200)))))
Run Code Online (Sandbox Code Playgroud)

Tay*_*ood 5

您可以使用返回值的可变集合,然后在每次调用时返回/删除它的值.

(defn foo [x] (inc x)) ;; example fn to be mocked
Run Code Online (Sandbox Code Playgroud)

如果你想模拟三个foo分别返回1,2和3的调用:

(with-redefs [foo (let [results (atom [1 2 3])]
                    (fn [_] (ffirst (swap-vals! results rest))))]
  (prn (foo 0))
  (prn (foo 0))
  (prn (foo 0))
  ;; additional calls would return nil
  (prn (foo 0)))
;; 1
;; 2
;; 3
;; nil
Run Code Online (Sandbox Code Playgroud)

这用于swap-vals!获取原子的旧/新值,但需要Clojure 1.9或更高版本.

如果你没有,swap-vals!你可以这样做(更少原子):

(with-redefs [foo (let [results (atom [1 2 3])]
                    (fn [_]
                      (let [result (first @results)]
                        (swap! results rest)
                        result)))]
  ...)
Run Code Online (Sandbox Code Playgroud)