by Eunmin Kim
실행 중에 특정 함수를 다른 함수로 교체하고 싶습니다.
(require '[org.httpkit.client :as http])
(defn stub-get [url]
{:status 200 :headers {} :body "Hello World"})
(deftest test-get
(with-redefs [http/post stub-get]
(is (= "Hello World" (:body (http/get "http://service.com/greet"))))))
(defn- var-symbol [var]
(symbol (str (.ns var)) (str (.sym var))))
(defn- var-map [source-ns target-ns]
(let [target (ns-interns target-ns)]
(reduce (fn [r [k v]]
(let [target-v (get target k)]
(if (and target-v (not (:macro (meta v))))
(conj r v target-v)
r)))
[]
(ns-interns source-ns))))
(defmacro with-ns-redefs [bindings & body]
(let [bindings (map var-symbol (mapcat (fn [[s t]]
(var-map s t))
(partition 2 bindings)))]
`(with-redefs ~bindings
~@body)))
(require '[org.httpkit.client :as http])
(require '[myproject.mock :as mock])
(deftest test-get
(with-ns-redefs [org.httpkit.client myproject.mock]
(is (= "Hello World" (:body (http/get "http://service.com/greet"))))))