我已经实现了更新功能的重试策略(与数据库交谈) - 如果更新抛出异常,我会重试10次。我正在用midje嘲笑更新功能。我想模拟它第一次失败,第二次成功。我试过这个:
(fact "update is retried when failed"
(ud/ensure-data {:username ..username.. :data :h}) => ..result..
(provided
(ud/get-raw-user-data ..username..) => example-user-data-raw
(ud/update-user-data {:username ..username..
:version 1
:userdata {:data {:h {}}}}) =throws=> (Exception.)
(ud/update-user-data {:username ..username..
:version 1
:userdata {:data {:h {}}}}) => ..result..))
但这似乎不起作用......回应是:
These calls were not made the right number of times:
(ud/update-user-data {:username ..username.., :version 1, :userdata {:homebases {:h {:sensors []}}}}) [expected at least once, actually never called]
我也找到了流(https://github.com/marick/Midje/wiki/Variant-prerequisite-arrows),但我不知道如何将异常与成功调用与流组合。
答案 0 :(得分:0)
我还没有清楚地了解如何使用Midje流。因此,我的个人解决方案是,不要使用provided
或'流',而是使用with-redefs
和'存根'。
(defn fn0 [] -1) ;; function we want to mock
;; fn1 retries fn0 if there is an exception
(defn fn1 [] (try
(fn0)
(catch Exception e
(do
(prn "Exception Caught, try again...")
(fn0)))))
(def call-count (atom -1)) ;; counts how many times fn0 is called
;; stub fn0 by returning different result
(defn make-stub [& result-seqs]
(fn [& _]
(swap! call-count inc)
(let [result (nth result-seqs @call-count)]
(if (instance? Throwable result)
(throw result)
result))))
(fact "fn1 ignores first exception and returns second call to fn0"
(with-redefs [fn0 (make-stub (Exception. "stubbed error") 100)]
(fn1) => 100))