Unit testing 如何在Clojure中测试抛出的异常?
在测试代码时,我也喜欢涵盖失败的情况 我是Clojure的新手,但是否有可能测试抛出的异常 我想测试以下示例代码:Unit testing 如何在Clojure中测试抛出的异常?,unit-testing,exception,testing,exception-handling,clojure,Unit Testing,Exception,Testing,Exception Handling,Clojure,在测试代码时,我也喜欢涵盖失败的情况 我是Clojure的新手,但是否有可能测试抛出的异常 我想测试以下示例代码: (ns com.stackoverflow.clojure.tests) (defn casetest [x] (case x "a" "An a." "b" "A b." (-> (clojure.core/format "Expression '%s' not defined." x) (IllegalArgumentExce
(ns com.stackoverflow.clojure.tests)
(defn casetest [x]
(case x
"a" "An a."
"b" "A b."
(-> (clojure.core/format "Expression '%s' not defined." x)
(IllegalArgumentException.)
(throw))))
采用以下试验方法:
(ns com.stackoverflow.clojure.tests-test
(:require [clojure.test :refer :all]
[com.stackoverflow.clojure.tests :refer :all]))
(deftest casetest-tests
(is (= "An a." (casetest "a")))
(is (= "A b." (casetest "b")))
(throws-excpeption IllegalArgumentException. (casetest "c")) ; conceptual code
)
(run-tests)
My project.clj看起来像:
(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
:description "Tests of Clojure test-framework."
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]])
您可以使用
(is(抛出?ExpectedExceptionClass正文))
看