1

我已经为更新功能(与数据库通信)实施了重试策略 - 如果更新引发异常,我最多重试 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),但我不知道如何将异常与成功调用与流结合起来。

4

1 回答 1

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))
于 2015-07-21T11:29:35.667 回答