2017-03-28 55 views
1

工作不繼的clojure.test源文檔字符串,我發出的REPL如下:clojure.test拋出?在REPL

(use 'clojure.test) 
(is (thrown? ArithmeticException (/ 1 0))) 

和其他變化就像java.lang.ArithmeticExceptionArithmeticException。然而,這個例外從來沒有被clojure.test捕獲,而是一路冒出來:

> #error { :cause "Divide by zero" :via [{:type java.lang.ArithmeticException :message "Divide by zero" :at 
> [clojure.lang.Numbers divide "Numbers.java" 158]}] :trace 
> [[clojure.lang.Numbers divide "Numbers.java" 158] 
> [clojure.lang.Numbers divide "Numbers.java" 3808] [api.core$eval1305 
> invokeStatic "form-init8750388124499546857.clj" 1] 
> [api.core$eval1305 invoke "form-init8750388124499546857.clj" 1] 
> [clojure.lang.Compiler eval "Compiler.java" 6927] 
> [clojure.lang.Compiler eval "Compiler.java" 6890] [clojure.core$eval 
> invokeStatic "core.clj" 3105] [clojure.core$eval invoke "core.clj" 
> 3101] [clojure.main$repl$read_eval_print__7408$fn__7411 invoke 
> "main.clj" 240] [clojure.main$repl$read_eval_print__7408 invoke 
> "main.clj" 240] [clojure.main$repl$fn__7417 invoke "main.clj" 258] 
> [clojure.main$repl invokeStatic "main.clj" 258] [clojure.main$repl 
> doInvoke "main.clj" 174] [clojure.lang.RestFn invoke "RestFn.java" 
> 1523] 
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__667 
> invoke "interruptible_eval.clj" 87] [clojure.lang.AFn applyToHelper 
> "AFn.java" 152] [clojure.lang.AFn applyTo "AFn.java" 144] 
> [clojure.core$apply invokeStatic "core.clj" 646] 
> [clojure.core$with_bindings_STAR_ invokeStatic "core.clj" 1881] 
> [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1881] 
> [clojure.lang.RestFn invoke "RestFn.java" 425] 
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate 
> invokeStatic "interruptible_eval.clj" 85] 
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke 
> "interruptible_eval.clj" 55] 
> [clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__712$fn__715 
> invoke "interruptible_eval.clj" 222] 
> [clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__707 
> invoke "interruptible_eval.clj" 190] [clojure.lang.AFn run 
> "AFn.java" 22] [java.util.concurrent.ThreadPoolExecutor runWorker 
> "ThreadPoolExecutor.java" 1142] 
> [java.util.concurrent.ThreadPoolExecutor$Worker run 
> "ThreadPoolExecutor.java" 617] [java.lang.Thread run "Thread.java" 
> 745]]} 

它可能是什麼? clojure.test中的其他內容可以在預期的repl中工作。

版本信息:

Leiningen 2.7.1上的Java 1.8.0_101爪哇熱點(TM)64位服務器VM

> nREPL server started on port 44025 on host 127.0.0.1 - 
> nrepl://127.0.0.1:44025 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 Java 
> HotSpot(TM) 64-Bit Server VM 1.8.0_101-b13 
+0

謝謝你們。看起來像https://clojure.github.io/clojure/clojure.test-api.html上的文檔字符串需要修復,不是嗎? – matanster

回答

1

使用它/敷在(clojure.test/deftest...)


假設:

(require '[clojure.test :as t]) 

通例如

(t/deftest a 
    (t/testing "a test" 
    (t/is (thrown? ArithmeticException (/ 1 0))))) 

(a) 
nil 

失敗例子

(t/deftest a 
    (t/testing "a test" 
    (t/is (thrown? NullPointerException (/ 1 0))))) 

(a) 
; ERROR in (a) (Numbers.java:158) 
; a test 
; expected: (thrown? NullPointerException (/ 1 0)) 
; actual: java.lang.ArithmeticException: Divide by zero 

你也可以用t/run-tests獲得摘要/統計


或者似乎可以用t/test-var可怕的ctly像這樣

(t/test-var (t/is (thrown? ArithmeticException (/ 1 0)))) 
nil 
+0

我不知道爲什麼要這樣做。 https://clojure.github.io/clojure/clojure.test-api.html上的文檔字符串是否需要改進才能避免這種誤解? – matanster

+1

@matanster,因爲只有在test-var中才有實際的try-catch – birdspider

1

您還可能有興趣在throws?宏觀 from the Tupelo library。它比clojure.test中的thrown?子句更通用並且更容易使用 。請注意,clojure.test中的thrown?不是正常的 Clojure函數或宏,但是是僅在is宏中識別的自定義表達式)。

tupelo.test/throws?宏是一個正常的Clojure宏,可以在任何地方使用。

throws?是爲了單獨使用,不應該被包裝在is聲明中。 請注意,throws?始終返回truefalse,可用於進一步處理。

> lein repl 
user=> (use 'tupelo.test) 
user=> (throws? (/ 1 0)) 
true ; <= return value 

user=> (throws? Exception (/ 1 0)) 
true ; <= return value 

user=> (throws? ArithmeticException (/ 1 0)) 
true ; <= return value 

如果表達式不拋出異常,或thows不同類型的異常,那麼 throws?將通過(clojure.test/is false)註冊失敗的測試,並且也將返回false

user=> (throws? NullPointerException (/ 1 0)) 
FAIL in() (form-init2868942318552383212.clj:1) 
expected: (try (/ 1 0) false (catch NullPointerException t1__13775__auto__ true) (catch java.lang.Throwable t2__13776__auto__ false)) 
    actual: false 
false ; <= return value 

clj.core=> (throws? ArithmeticException (+ 1 0)) 
FAIL in() (form-init4299343763551622058.clj:1) 
expected: (try (+ 1 0) false (catch ArithmeticException t1__13761__auto__ true) (catch java.lang.Throwable t2__13762__auto__ false)) 
    actual: false 
false ; <= return value 

clj.core=> (throws? (+ 1 0)) 
FAIL in() (form-init4299343763551622058.clj:1) 
expected: (try (+ 1 0) false (catch java.lang.Throwable t3__13763__auto__ true)) 
    actual: false 
false ; <= return value 

典型tupelo.core自己的單元測試說明了使用throws?

(ns test.tupelo.core 
    (:use clojure.test tupelo.test) 
    (:require [tupelo.core :as t])) 
(t/refer-tupelo) 

(deftest t-grab 
    (let [map1 {:a 1 :b 2}] 
    (is= 1 (grab :a map1)) 
    (is= 2 (grab :b map1)) 
    (throws? (grab :c map1)))) 
+1

你好Alan,你總是熱衷於在這裏分享Tupelo。沒有什麼不對,但我認爲這是很好的透露你是作者。 https://stackoverflow.com/help/behavior – glts

相關問題