mockfn icon indicating copy to clipboard operation
mockfn copied to clipboard

Add an API for declaring a series of return values

Open dchelimsky opened this issue 3 years ago • 0 comments

I just wrote something like this in a project to supply different return values each time a function is called:

(def times (atom []))

(defn set-times [new-times]
  (reset! times (into [] (reverse new-times))))

(defn next-time []
  (let [ret (peek @times)]
    (swap! times pop)
    ret))

(deftest test-times
  (set-times [#nu/time "2022-04-01T10:30:00Z"
              #nu/time "2022-04-01T10:30:01Z"])
  (mfn/providing [(time/now) (mfn/calling next-time)]
    (is (= #nu/time "2022-04-01T10:30:00Z" (time/now)))
    (is (= #nu/time "2022-04-01T10:30:01Z" (time/now)))))

I'd be great to just write something like this:

(deftest test-times
  (mfn/providing [(time/now) (mfn/returning #nu/time "2022-04-01T10:30:00Z"
                                            #nu/time "2022-04-01T10:30:01Z")]
    (is (= #nu/time "2022-04-01T10:30:00Z" (time/now)))
    (is (= #nu/time "2022-04-01T10:30:01Z" (time/now)))))

dchelimsky avatar Apr 07 '22 20:04 dchelimsky