Compojure应用程序不能很好地与with-redefs [英] Compojure app not playing well with with-redefs

查看:160
本文介绍了Compojure应用程序不能很好地与with-redefs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Compojure应用程序,并使用 clj-webdriver 来图形测试。我试图使用 with-redefs 模拟出从持久性中拉出数据的函数,只是返回罐装值,但它忽略了我的函数覆盖。我知道 with-redefs 在vars方面有效,但它仍然不工作:

I'm writing a Compojure application and am using clj-webdriver to graphically test it. I'm trying to use with-redefs to mock out the function that pulls out data from persistence to just return canned values, but it's ignoring my function overwrite. I know with-redefs works in terms of vars, but it's still not working:

project.clj相关的:

project.clj relevant pieces:

(defproject run-hub "0.1.0-SNAPSHOT"                                                                                                                        
  :main run-hub.handler/start-server)

handler.clj:

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

log-controller.clj:

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



.clj

persistence.clj

(ns run-hub.persistence                                                                                                                                     
  (require [clj-time.core :as time]
           [run-hub.models.log :as log]))

(defn mikes-log [] [])

最后,我的图形测试 - 尝试覆盖 mikes-log ,失败:

And finally, my graphical test - which tries to override mikes-log and fails:

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

其中一周代码片段是返回一些样本数据的函数。
(defn start-server []
(run-jetty(var app){:port 3000:join?false}))

Where one-week-snippet is a function that returns some sample data. (defn start-server [] (run-jetty (var app) {:port 3000 :join? false}))

推荐答案

我可以在 clj-webdriver 测试中使用 with-redefs 如下:

I am able to use with-redefs in a clj-webdriver test doing the following:

(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 工作。

这篇关于Compojure应用程序不能很好地与with-redefs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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