簡體   English   中英

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應該符合您的用法。

http://clojuredocs.org/clojure.core/with-redefs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM