如何避免在Clojure中进行单元测试时手动评估defrecords和defprotocols? [英] How to avoid having to manually evaluate defrecords and defprotocols while unit testing in Clojure?

查看:138
本文介绍了如何避免在Clojure中进行单元测试时手动评估defrecords和defprotocols?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Clojure代码库中,我定义了几个协议和几个defrecords。我使用clojure.test单元测试在我的defrecords中定义的具体函数。



例如,我有以下源文件:



在src / foo / protocols.clj中:

 (ns foo.protocols)

(defprotocol RequestAcceptability
(desired-accepted-charset [this]))

在src / foo / types.clj中:

 (ns foo.types 
(:use [foo .protocols:only [RequestAcceptability desired-accepted-charset]])

(defrecord RequestProcessor
[field-1 field-2]
RequestAcceptability
-charset [this]
...实现在这里...))

/foo/types_test.clj:

 (ns foo.types-test 
(:use [clojure.test] )
(:use [foo.protocols:only [RequestAcceptability desired-accepted-charset]])
(:import [foo.types RequestProcessor]))

(deftest test -desired-accepted-charset_1
...在这里测试代码...)

我使用Clojure 1.4,Leiningen 2,nrepl在Emacs。



我面对的烦恼是,当我去运行我的单元测试(例如,使用Cc C-,序列),我得到一个 ClassNotFoundException:foo .types.RequestProcessor 。为了解决这个问题,我做手动工作,评估,我个人,我的每个协议和defrecord形式。即,我将浏览到我的protocols.clj并评估(C-M-x键序列为nrepl)我的defprotocol形式;然后我将导航到我的types.clj并评估我的defrecord形式;然后最终我能够成功地运行我的单元测试w / out得到ClassNotFoundException。



当然,在我真正的代码库,我必须这样做所有我的协议和defrecords,所以它是非常乏味和耗时。此外,如果我简单地放到一个shell并执行 lein测试,我得到相同的ClassNotFoundException。



有更好的方法吗?感谢您的时间和帮助。

解决方案

您需要 require 在测试命名空间中包含defrecord的命名空间。



(:import [foo .types RequestProcessor]))将不会自己工作,因为它只适用于类路径上已经存在的Java类。这不是这里的情况,因为您使用defrecord在运行时动态创建类。



import 通常只需要Java互操作。


In my Clojure codebase, I have defined several protocols and several defrecords. I am using clojure.test to unit test the concrete functions defined within my defrecords.

For example, let's say I have the following source files:

In src/foo/protocols.clj:

(ns foo.protocols)

(defprotocol RequestAcceptability
  (desired-accepted-charset [this]))

In src/foo/types.clj:

(ns foo.types
  (:use [foo.protocols :only [RequestAcceptability desired-accepted-charset]])

(defrecord RequestProcessor
  [field-1 field-2]
  RequestAcceptability
  (desired-accepted-charset [this]
    ...implementation here...))

In test/foo/types_test.clj:

(ns foo.types-test
  (:use [clojure.test])
  (:use [foo.protocols :only [RequestAcceptability desired-accepted-charset]])
  (:import [foo.types RequestProcessor]))

(deftest test-desired-accepted-charset_1
  ...test code here...)

I am using Clojure 1.4, Leiningen 2, nrepl within Emacs.

The annoyance I am facing is that when I go to run my unit tests (e.g., using C-c C-, sequence), I get a ClassNotFoundException: foo.types.RequestProcessor. To get around this, I'm doing the manual work of evaluating, individually, each of my protocol and defrecord forms. I.e., I'll navigate over to my protocols.clj and evaluate (C-M-x key sequence for nrepl) my defprotocol form; and then I'll navigate to my types.clj and evaluate my defrecord form; and then finally I'm able to successfully run my unit tests w/out getting the ClassNotFoundException.

Of course, in my real code base, I have to do this for all my protocols and defrecords, and so it is very tedious and time consuming. Also, if I simply drop to a shell and do lein test, I get the same ClassNotFoundException.

Is there a better way?

Thank you for your time and help.

解决方案

You need to require the namespace that contains the defrecord in your test namespace.

(:import [foo.types RequestProcessor])) won't work on its own, since that only works for Java classes that already exist on the classpath. This isn't the case here, since you are using defrecord to dynamically create the class at runtime.

import is usually only needed for Java interop.

这篇关于如何避免在Clojure中进行单元测试时手动评估defrecords和defprotocols?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆