with-redefs不会重新定义我的函数 [英] with-redefs doesn't redefine my function

查看:172
本文介绍了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)))

所以,基本上我想停止对数据库命名空间的调用,但我最终upp在胖数据库exeception stacktrace .. 。

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

有什么问题?

推荐答案

with-redefs-fn 获取绑定的映射,而不是向量。请注意,clojuredocs中的示例使用#'读取器宏来引用Var,因此总结可以尝试

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

这篇关于with-redefs不会重新定义我的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆