我正在编写一个 Compojure 应用程序并使用clj-webdriver
它来进行图形测试。我试图用来with-redefs
模拟从持久性中提取数据以仅返回预设值的函数,但它忽略了我的函数覆盖。我知道with-redefs
在 vars 方面有效,但它仍然不起作用:
project.clj 相关部分:
(defproject run-hub "0.1.0-SNAPSHOT"
:main run-hub.handler/start-server)
处理程序.clj:
(ns run-hub.handler
(:require [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[ring.adapter.jetty :refer :all]
[run-hub.controllers.log-controller :as log-controller]))
(defroutes app-routes
(GET "/MikeDrogalis/log" [] (log-controller/mikes-log))
(route/resources "/")
(route/not-found "Not Found"))
(def app (handler/site #'app-routes))
日志控制器.clj:
(ns run-hub.controllers.log-controller
(:require [run-hub.views.log :as views]
[run-hub.persistence :as persistence]))
(defn mikes-log []
(views/mikes-log (persistence/mikes-log)))
持久性.clj
(ns run-hub.persistence
(require [clj-time.core :as time]
[run-hub.models.log :as log]))
(defn mikes-log [] [])
最后,我的图形测试 - 它试图覆盖mikes-log
并失败:
(fact
"It has the first date of training as August 19, 2012"
(with-redefs [persistence/mikes-log (fn [] (one-week-snippet))]
(to (local "/MikeDrogalis/log"))
(.contains (text "#training-log") "August 19, 2012"))
=> true)
Whereone-week-snippet
是一个返回一些样本数据的函数。(defn start-server [] (run-jetty (var app) {:port 3000 :join?false}))