1.2.0 permalink Arrow_down_16x16
  • (expect expect-bindings & body)
Use expect to redirect calls to dependent functions that are made within the
code under test. Instead of calling the functions that would normally be used,
temporary stubs are used, which can verify function parameters and call counts.
Return values can also be specified as needed.
Usage:
(expect [dep-fn (has-args [arg-pred1] (times n (returns x)))]
(function-under-test a b c))

1 Example top

  • (defn square [x] (* x x))
    
    (defn square-of-sum [x y] (square (+ x y)))
    
    (deftest test-square-of-sum
      (expect [square (has-args [3] (times 1))]
        (square-of-sum 2 1)))
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/mock.clj:265 top

(defmacro expect
 "Use expect to redirect calls to dependent functions that are made within the
code under test. Instead of calling the functions that would normally be used,
temporary stubs are used, which can verify function parameters and call counts.
Return values can also be specified as needed.
Usage:
(expect [dep-fn (has-args [arg-pred1] (times n (returns x)))]
  (function-under-test a b c))"

  [expect-bindings & body]
  (assert-args expect
    (vector? expect-bindings) "a vector of expectation bindings"
    (even? (count expect-bindings))
    "an even number of forms in expectation bindings")
  (let [mock-data (gensym "mock-data_")]
    `(let [~mock-data (map (fn [args#]
                             (apply clojure.contrib.mock/make-mock args#))
                        ~(cons 'list (map (fn [[n m]] (vector (list 'quote n) m))
                                       (partition 2 expect-bindings))))]
       (binding ~(make-bindings expect-bindings mock-data) ~@body)
       (clojure.contrib.mock/validate-counts ~mock-data) true)))
Used in 0 other vars

Comments top

No comments for expect. Log in to add a comment.