Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何在Clojure中测试抛出的异常?_Unit Testing_Exception_Testing_Exception Handling_Clojure - Fatal编程技术网

Unit testing 如何在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

在测试代码时,我也喜欢涵盖失败的情况

我是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)
        (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正文))