unit testing - clojure.test (is (thrown? ...) not seeing exception -
i have function param-values
throws illegalargumentexception
when cannot find key in liberator context. have clojure.test unit test this:
(testing "non-existing key" (is (thrown? illegalargumentexception (param-values ctx [:baz]))))
for reason, test failing, though can see function behaving correctly in repl:
user> (param-values ctx [:baz]) illegalargumentexception missing required param baz resources/param-value (resources.clj:57) user> (is (thrown? illegalargumentexception (param-values ctx [:baz]))) fail in clojure.lang.persistentlist$emptylist@1 (form-init2687593671136401208.clj:1) expected: (thrown? illegalargumentexception (param-values ctx [:baz])) actual: nil
param-values
quite simple; maps on specified args param-value
:
(defn param-values [ctx args & [{:keys [optional-args] :as opts}]] (let [params (or (get-in ctx [:request :params]) {}) args (concat args optional-args)] (map #(param-value params % opts) args)))
of course, have more in-depth tests param-value
, 1 of is:
(testing "arg not present" (is (thrown-with-msg? illegalargumentexception #"missing required param baz" (param-value {} :baz))))
this test passes!
what gives? i'm doing not jiving clojure.test/is
macro? have typo i'm thick see?
is because param-values
returns lazy sequence?
when using thrown-with-msg?
calls re-find
on return value of (param-value {} :baz)
, evaluates lazy sequence. not happen when using thrown?
instead.
so try instead:
(is (thrown? illegalargumentexception (doall (param-values ctx [:baz]))))
doall
evaluate lazy sequence.
update based on comments:
as not interested in return value of lazy sequence, side effects (throwing exception), it's preferred use dorun
instead of doall
.
Comments
Post a Comment