unit-testingclojuremidje

How do I test futures in clojure?


I'm trying to use midje to test a future, but I can't seem to get it to work.

The code looks like

(defn foo []
  (let [f (future (bar))]
    (baz @f))

With a test like

(fact 
  (foo) => ..a..
  (provided
    (bar) => ..b..
    (baz ..b..) => ..a..))

This fails, saying that bar never gets called. Any way around this?


Solution

  • This code shows that midje test wait until the dereferencing future var, so i think midje with futures is not your problem but other in the sequence functions you have

    (defn foo []
      (let [state (future (Thread/sleep 1000) (println "done") "done")]
          @state
        )
    
      )
    (fact "description of the fact"
          "done" => (foo))
    ==>true
    

    Good luck!