is 的 clojure 测试用例断言失败
clojure test case assertion failure for is
我的源代码中有一个简单的函数 hello
。
(defn hello [n] (str "Hello" n))
在测试用例中,我调用了 hello
函数。但似乎是 returning 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"))
为什么 returning nil
?如果我从 repl 调用 hello
函数,我得到 "Hello x" 后跟 nil
,但我认为这是由于 repl 对吧?当我从另一个函数调用 hello
时,它不应该是 return 字符串吗?我是 运行 直接从 repl 测试用例,没有使用 lein。
根据您的描述,您的实际 hello
函数似乎定义为:
(defn hello [n]
(println "Hello" n))
当您 运行 (hello "x")
时,它会向控制台打印 Hello x
和 returns 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] (str "Hello" n))
在测试用例中,我调用了 hello
函数。但似乎是 returning 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"))
为什么 returning nil
?如果我从 repl 调用 hello
函数,我得到 "Hello x" 后跟 nil
,但我认为这是由于 repl 对吧?当我从另一个函数调用 hello
时,它不应该是 return 字符串吗?我是 运行 直接从 repl 测试用例,没有使用 lein。
根据您的描述,您的实际 hello
函数似乎定义为:
(defn hello [n]
(println "Hello" n))
当您 运行 (hello "x")
时,它会向控制台打印 Hello x
和 returns 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=>