2

私は例外のスローをテストしようとします:

;;; src
;; Courtesy https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj
(defmacro assert-args [& pairs]
  `(do (when-not ~(first pairs)
         (throw (IllegalArgumentException.
                 (str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
       ~(let [more (nnext pairs)]
          (when more
            (list* `assert-args more)))))

(defmacro my-macro []
  (assert-args false "foobar")
  #_(...))

;;; test
(deftest x-fn-test
  (is (thrown-with-msg? IllegalArgumentException #"foo" (my-macro))))

ただし、テスト フレームワークによってキャッチされる例外はなく、すべてスローされます。

4

2 に答える 2

2

あなたのthrow表現は奇形です、あなたが望む

(throw (IllegalArgumentException. "foo"))

完全な作業コード:

(ns mytest.core
  (:use clojure.test))

(defn x-fn []
  (throw (IllegalArgumentException. "foo")))

(deftest x-fn-test
  (is (thrown-with-msg? IllegalArgumentException           #"foo"     (x-fn)))
  (is (thrown-with-msg? IllegalArgumentException           #".*foo.*" (x-fn)))
  (is (thrown-with-msg? IllegalArgumentException           #"^foo$"   (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #"foo"     (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #".*foo.*" (x-fn)))
  (is (thrown-with-msg? java.lang.IllegalArgumentException #"^foo$"   (x-fn))))


; nREPL 0.1.8-preview
user> 
#<Namespace mytest.core>
mytest.core> (run-tests)

Testing mytest.core

Ran 1 tests containing 6 assertions.
0 failures, 0 errors.
{:type :summary, :pass 6, :test 1, :error 0, :fail 0}
于 2013-06-28T16:49:12.193 に答える