简体   繁体   中英

with-redefs doesn't redefine my function

I have a test:

(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))))))

and the code that are under test:

(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)))

So, basically i'd like to stop the call to the database-namespace, but i end upp in a fat database exeception stacktrace...

What is wrong?

with-redefs-fn takes a map of bindings, not a vector. Note that the examples at clojuredocs use the #' reader macro to refer to the Var, so summing up you could try

(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))))))

Take a look at with-redefs it should match your usage.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM