2016-10-08 3 views
0

私のソースには単純な関数helloがあります。テストケースにはclojureテストケースアサーションの失敗は

(defn hello [n] (str "Hello" n)) 

私はhello関数を呼び出しています。しかし、それは返すように思われるnil。テストケース:

(deftest test (testing "string" 
    ;(is (= "Hello x" (ql/hello "x"))))) 
    (is (= (ql/hello "x") "Hello x")))) 

次のエラーが発生します。

expected: (= (ql/hello "x") "Hello x") 
    actual: (not (= nil "Hello x")) 

nilはなぜ返されますか?私がhello関数をreplから呼び出すと、「Hello x」の後にnilが続きますが、私はそれが正しいと考えていますか? helloを別の関数から呼び出すと、文字列を返すべきではありませんか?私はreplからテストケースを直接実行しており、leinは使用していません。

+0

私はreplで 'nil'を取得しませんでした。 – qxg

答えて

3

あなたの説明から、あなたの実際のhello機能は次のように定義されたようだ。(それはprintlnの行動だ)あなたが(hello "x")を実行すると、それはコンソールにHello xを出力しnilを返し

(defn hello [n] 
    (println "Hello" n)) 

あなたのテストを合格させるには、printlnではなく、strのバージョンと一致するようにREPLで関数を再定義する必要があります。

boot.user=> (require '[clojure.test :refer :all]) 
nil 
boot.user=> (defn hello [n] 
     #_=> (println "Hello" n)) 
#'boot.user/hello 
boot.user=> (is (= "Hello x" (hello "x"))) 
Hello x 

FAIL in() (boot.user5664236129068656247.clj:1) 
expected: (= "Hello x" (hello "x")) 
    actual: (not (= "Hello x" nil)) 
false 
boot.user=> (defn hello [n] 
     #_=> (str "Hello " n)) 
#'boot.user/hello 
boot.user=> (is (= "Hello x" (hello "x"))) 
true 
boot.user=>