我有一些固定装置可以启动并关闭项目中的数据库。
现在看起来像这样:
(use-fixtures :once with-embedded-db)
在灯具本身中,我有一个动态变量,可以在不同的地方使用它:
(def ^:dynamic *db*)
(defn with-embedded-db [f]
(binding [*db* (db/connect args)]
(f)
(finally
(db/clean-up *db)))
现在,假设
db/connect
和db/clean-up
需要一些时间。问题:
当我使用
lein test
运行测试时,会花费很长时间,从而不必要地花时间在每个名称空间的数据库连接和断开连接上。题:
有没有办法设置全局固定装置,以便当我运行
lein test
时,它为所有测试名称空间仅调用一次?谢谢!
最佳答案
如果将功能添加到Leiningen本身会更好。如果不是PR,则至少应打开一张票。
以下解决方案是肮脏的,但是您可以理解它并将其转换为更智能的东西。
;; profect.clj
:profiles
{:dev {:dependencies [[robert/hooke "1.1.2"]]
:injections [(require '[robert.hooke :as hooke])
(defn run-all-test-hook [f & nss]
(doall (map (fn [a]
(when (intern a '*db*)
(intern a '*db* "1234"))) nss))
(apply f nss))
(hooke/add-hook #'clojure.test/run-tests #'run-all-test-hook)
]}}
注意:莱宁根本身在其核心中使用了罗伯特/胡克。
然后在测试中的某个地方:
(ns reagenttest.cli
(:require [clojure.test :refer :all]))
(def ^:dynamic *db*) ;; should be defined in every NS where it is needed
(deftest Again
(testing "new"
(prn *db*)))