with-redefs 没有重新定义我的函数
with-redefs doesn't redefine my function
我有一个测试:
(ns gui-proxy.handler-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[gui-proxy.handler :as handler]))
(deftest test-app
(testing "not-found route"
(with-redefs-fn [handler/log-request (fn [type url] (str ""))]
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
和正在测试的代码:
(ns gui-proxy.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[clj-http.client :as client]
[gui-proxy.db :as db]))
(defn log-request [type url]
(db/insert-request-info type url))
(defn log-error []
(log-request ":fail" "fail"))
"gui-proxy - File not found")
(defroutes app-routes
(route/not-found (log-error)))
所以,基本上我想停止对数据库命名空间的调用,但我最终陷入了一个胖数据库异常堆栈跟踪...
怎么了?
with-redefs-fn 采用绑定图,而不是矢量。请注意,clojuredocs 中的示例使用 #'
reader 宏来引用 Var,因此总结一下您可以尝试
(deftest test-app
(testing "not-found route"
(with-redefs-fn {#'handler/log-request (fn [type url] (str ""))}
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
看看 with-redefs
它应该符合您的用法。
我有一个测试:
(ns gui-proxy.handler-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[gui-proxy.handler :as handler]))
(deftest test-app
(testing "not-found route"
(with-redefs-fn [handler/log-request (fn [type url] (str ""))]
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
和正在测试的代码:
(ns gui-proxy.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[clj-http.client :as client]
[gui-proxy.db :as db]))
(defn log-request [type url]
(db/insert-request-info type url))
(defn log-error []
(log-request ":fail" "fail"))
"gui-proxy - File not found")
(defroutes app-routes
(route/not-found (log-error)))
所以,基本上我想停止对数据库命名空间的调用,但我最终陷入了一个胖数据库异常堆栈跟踪...
怎么了?
with-redefs-fn 采用绑定图,而不是矢量。请注意,clojuredocs 中的示例使用 #'
reader 宏来引用 Var,因此总结一下您可以尝试
(deftest test-app
(testing "not-found route"
(with-redefs-fn {#'handler/log-request (fn [type url] (str ""))}
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
看看 with-redefs
它应该符合您的用法。