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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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 is的clojure测试用例断言失败_Unit Testing_Clojure_Assertions - Fatal编程技术网

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