请容忍这个人为的例子,但这是我能想到的最简单的事情来重现这个问题。
(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})在REPL中或在lein run中运行以下内容很好。
(let [subject (make-something)
actual (call-foo subject "hello" "greetings")]
(println actual))
;;=> (hello greetings)此问题仅在此测试和执行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))))这会引发一个错误。一个示例输出:
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)“无法解析符号:上下文中的某些内容”这一行使我认为Something由于某种原因不在上下文中,而我在call-foo中使用。但为什么只有在测试中才会出现这种情况?
发布于 2014-07-17 15:49:30
问题是eval没有看到上下文。您的'Something在something.core和something.core-test中解析,因为您已经引用了所有内容。它不会从lein test运行其测试的任何名称空间解析。
修复当前问题的更改
'Something至
`Something所以它是命名空间限定的。然后测试将运行(并失败),但这是另一个问题(println返回nil )。
https://stackoverflow.com/questions/24806608
复制相似问题