我不熟悉Clojure中mocking是如何工作的。具体地说,我不确定应该如何测试实现sqlKorma查询或对数据库的调用的函数?理想情况下,我希望能够在测试中模拟sqlKorma。
(defn fetch [id]
(->
(korma/select* :my-table)
(korma/where {:id id})
(korma/exec)))
(defn retrieve [id]
(->
(fetch id)
(ring/response)))我正在使用Speclj来测试我的应用程序。
(describe "item"
(it "is fetched"
(let [fetched (fetch :test-case)] ;here I want to be able to mock sqlKorma and return an array of 1.
(should= (count fetch) 1)))
(it "is retrieved"
(let [retrieved (retrieve :test-case)]
(should= (get retrieved :status) 200))))发布于 2018-02-27 18:05:31
有几种方法。一种方法是使用不同的数据库进行测试。例如内存数据库中的H2。这是首选,因为您不需要mocking,而且您还需要测试您的SQL。如果您真的想模拟您的fetch函数,可以使用with-redefs
(defn foo [] [{:foo "bar"}])
(foo) ;;=> [{:foo "bar"}]
(with-redefs [foo (fn [] [{:something "else"}])] (foo))
;;=> [{:something "else"}]
(foo) ;;=> [{:foo "bar"}]https://stackoverflow.com/questions/49005345
复制相似问题