问题
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