问题描述
我在Clojure测试 deftype
和 defprotocol
,但我有点腌菜。
I'm testing deftype
and defprotocol
in Clojure, but I'm in a bit of a pickle.
我使用leiningen。我的核心文件( src / core.clj
)如下所示:
I'm using leiningen. My core file (src/core.clj
) looks like this:
(defprotocol Speaker
(say [speaker message]))
(deftype Person [name]
Speaker
(say [_ message] (str name ": " message)))
我的测试文件( test / core.clj
)看起来像这样:
My test file (test/core.clj
) looks like this:
(deftest people-can-talk
(is (= "Peter: hello" (say (Person. "Peter") "hello"))))
当我执行那个测试( lein test
)时,我得到以下错误:
When I execute that test (with lein test
) I get the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to resolve classname: Person, compiling:(my-project/test/core.clj:2)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6416)
at clojure.lang.Compiler.analyze(Compiler.java:6216)
$ b b
我相信它告诉我 Person
没有定义。但它是!如果不是,Clojure不会抛出一个错误?是否有一些明显的语法错误我错过了?
I believe it is telling me that Person
isn't defined. But it is! If it wasn't, wouldn't Clojure have thrown an error? Is there some obvious syntax error I'm missing?
我使用clojure 1.3.0。
I'm using clojure 1.3.0 .
推荐答案
要在另一个包/文件中使用由deftype定义的类,您需要(导入)它们,或者提供完整的包名称,如(core.Person。
To use classes defined by deftype in another package/file, you need to (import) them, or provide the full package name, like (core.Person. "Peter")
- or whatever the namespace for core.clj is.
此外,lein test只加载测试文件。如果要引用另一个文件中的任何内容,则需要在测试文件中使用或要求该文件。
Additionally, "lein test" only loads the test files. If you want to refer to anything in another file, you'd need to use or require that file in the test file.
这篇关于Clojure - deftype被忽略 - 无法在测试中解析类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!