我有一个想要测试的异步基座拦截器:
(def my-interceptor
(io.pedestal.interceptor/interceptor
{:name :my-interceptor
:enter (fn [context]
(as/go
(do
(Thread/sleep 1000)
(assoc context :answer 42))))}))我首先尝试了一个天真的测试:
(deftest my-test
(is (= 42
(:answer (io.pedestal.interceptor.chain/execute {} [my-interceptor])))))这不起作用,因为当chain/execute有异步拦截器时,它会返回nil。我尝试了另一个解决方案,将测试添加到拦截器中,紧跟在测试的拦截器之后:
(deftest my-test
(io.pedestal.interceptor.chain/execute
{}
[my-interceptor
(io.pedestal.interceptor/interceptor
{:name :test
:enter (fn [context]
(is (= 41 (:answer context))) ; should fail
context)})]))然而,这并不起作用,因为测试在执行之前就终止了,因此成功了…即使测试在一秒后失败:
Ran 1 test containing 0 assertions.
No failures.
FAIL in (my-test) (somefile_test.clj:49)
expected: (= 41 (:answer context))
actual: (not (= 41 42))实际上,我的测试套件(使用Kaocha)失败了,因为其中有一个没有断言的deftest。
由于chain/execute返回的是nil而不是chan,所以在它终止之前,我不能将它包装在as/<!!中以进行阻塞。
在这一点上我被卡住了。我能做些什么来测试这种拦截器吗?
发布于 2020-04-12 08:38:15
这种方法怎么样?
(require '[clojure.test :as test])
(require '[clojure.core.async :as async)
(test/deftest async-test []
(let [c (async/chan)]
(future (println "Running mah interceptors") (async/>!! c :done))
(test/is (= :done (async/<!! c)))
(async/close! c)))在单独的线程中运行实际的拦截器代码。测试代码只需要在完成后将某些内容发布到c。
https://stackoverflow.com/questions/61137932
复制相似问题