Unit testing is的clojure测试用例断言失败
我的源代码中有一个简单的函数Unit testing is的clojure测试用例断言失败,unit-testing,clojure,assertions,Unit Testing,Clojure,Assertions,我的源代码中有一个简单的函数hello (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
hello
(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
?如果我从repl调用hello
函数,我会得到“hello x”,后跟nil
,但我认为这是由于repl造成的,对吗?当我从另一个函数调用hello
时,它不应该返回字符串吗?我直接从repl运行测试用例,而不是使用lein。根据您的描述,您实际的hello
函数被定义为:
(defn hello [n]
(println "Hello" n))
运行(hello“x”)
时,它将hello x
打印到控制台,并返回nil
(这是println
的行为)
要通过测试,您需要在REPL中重新定义函数,使其与您的版本匹配str
,而不是println
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=>
根据您的描述,您实际的
hello
函数定义如下:
(defn hello [n]
(println "Hello" n))
运行(hello“x”)
时,它将hello x
打印到控制台,并返回nil
(这是println
的行为)
要通过测试,您需要在REPL中重新定义函数,使其与您的版本匹配str
,而不是println
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=>
我在repl中没有得到
nil
。我在repl中没有得到nil
。