clojure.test 无法解析“eval”中的符号。在 REPL 和 lein run 中工作

Jam*_*mes 5 testing eval clojure

请忍受这个人为的例子,但这是我能想到的重现问题的最简单的方法。

(ns something.core)

(defn call-foo [something & args]
  (let [a-foo (:foo (eval (:quux something)))]
    (apply a-foo args)))

(def Something {
  :foo (fn [& args] args)
  :bar (fn [something] (call-foo something))
})

(defn make-something []
  {:quux 'Something})
Run Code Online (Sandbox Code Playgroud)

在 REPL 或 with 中运行以下命令lein run效果很好。

(let [subject (make-something)                      
      actual (call-foo subject "hello" "greetings")]
  (println actual))                                 
;;=> (hello greetings)
Run Code Online (Sandbox Code Playgroud)

该问题仅在测试和执行期间出现lein test

(ns something.core-test
  (:require [clojure.test :refer :all]
            [something.core :refer :all]))

(deftest a-test
  (let [subject (make-something)
        actual  (call-foo subject "hello" "greetings")]
    (is (= ["hello" "greetings"] actual))))
Run Code Online (Sandbox Code Playgroud)

这会引发错误。输出示例:

ERROR in (a-test) (Compiler.java:6464)
Uncaught exception, not in assertion.
expected: nil
  actual: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: Something in this context, compiling:(/private/var/folders/0n/c7q7860j34xfc2r1x4q51jrh0000gn/T/form-init9215140948330409114.clj:1:6436)
Run Code Online (Sandbox Code Playgroud)

“无法解析符号:此上下文中的某些Something内容”这一行让我认为,当我evalcall-foo. 但为什么只有测试的时候才会出现这种情况呢?

A. *_*ebb 4

问题是eval看不到上下文。自您提交所有内容以来,您就'Something做出了决定。它不会从运行测试的任何命名空间解析。something.coresomething.core-testlein test

解决当前问题更改

'Something
Run Code Online (Sandbox Code Playgroud)

`Something
Run Code Online (Sandbox Code Playgroud)

因此它是命名空间限定的。然后测试将运行(并失败),但这是另一个问题(一件事println返回)。nil