我正在编写Compojure应用程序并使用clj-webdriver
以图形方式测试它。我正在尝试使用with-redefs
来模拟从持久性中提取数据的函数,只返回固定值,但它忽略了我的函数覆盖。我知道with-redefs
在变量方面有效,但它仍无效:
project.clj相关部分:
(defproject run-hub "0.1.0-SNAPSHOT"
:main run-hub.handler/start-server)
handler.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))
登录controller.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)))
persistence.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)
其中one-week-snippet
是一个返回一些样本数据的函数。
(defn start-server []
(run-jetty(var app){:port 3000:join?false}))
答案 0 :(得分:0)
我可以在with-redefs
测试中使用clj-webdriver
执行以下操作:
(defn with-server
[f]
(let [server (run-jetty #'APP {:port 0 :join? false})
port (-> server .getConnectors first .getLocalPort)]
(binding [test-port port]
(try
(println "Started jetty on port " test-port)
(f)
(finally
(.stop server))))))
(use-fixtures :once with-server)
然后,一大堆测试得到了自己的码头,这似乎在运行
with-redefs
的工作方式。