Testing core.async code in ClojureScript

风格不统一 提交于 2019-12-11 16:27:53

问题


I'm trying to test some core.async code, but I'm having trouble writing tests. All of my test code is in a go block, but it seems like the process my test is on isn't parked in the way I expected it to be.

After I do the put on the actions channel, I would expect execution of the go block to be parked until the value is taken, but instead the is function runs straight away which causes my test to fail because the state-atom hasn’t been updated yet.

Here's my test code:

(deftest tab-change
  (async done
         (go (>! state/actions (state/TabChange. :return))
             (is (= @state/state-atom {:tab :return}))
             (done))))

And the code under test:

(def actions (async/chan))

(declare update)

(def state-atom (reagent/atom nil))

(go-loop []
  (swap! state-atom (partial update (<! actions)))
  (recur))

(defprotocol Action
  (update [action state]))

(defrecord TabChange [value]
  Action
  (update [_ state]
    (assoc state :tab value)))

If I change my test code to use put! and a callback, rather than a go block, the tests pass:

(deftest tab-change
  (async done
         (put! state/actions
               (state/TabChange. :return)
               #(do (is (= @state/state-atom {:tab :return}))
                    (done)))))

This obviously won't compose well for more complicated tests though, so I was hoping for some insight into what is causing the problem with the original test.

来源:https://stackoverflow.com/questions/33273545/testing-core-async-code-in-clojurescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!