2

我正在编写一个 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}))

4

1 回答 1

0

我可以在执行以下操作with-redefsclj-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运行。

于 2014-04-02T14:34:04.423 回答