【发布时间】:2017-02-26 07:32:17
【问题描述】:
在midje 框架内测试的process-async 函数产生不一致的结果。大多数情况下,它会按预期进行检查,但有时会在初始状态 ("") 读取 out.json。我依靠async-blocker 函数在检查之前等待process-async。
我的方法有什么问题?
(require '[[test-with-files.core :refer [with-files public-dir]])
(defn async-blocker [fun & args]
(let [chan-test (chan)]
(go (>! chan-test (apply fun args)))
(<!! chan-test)))
(defn process-async
[channel func]
(go-loop []
(when-let [response (<! channel)]
(func response)
(recur))))
(with-files [["/out.json" ""]]
(facts "About `process-async"
(let [channel (chan)
file (io/resource (str public-dir "/out.json"))
write #(spit file (str % "\n") :append true)]
(doseq [m ["m1" "m2"]] (>!! channel m))
(async-blocker process-async channel write)
(clojure.string/split-lines (slurp file)) => (just ["m1" "m2"] :in-any-order)
)
)
)
【问题讨论】:
标签: unit-testing clojure core.async midje