简体   繁体   English

自定义编解码器的Groovy / Grails单元测试错误

[英]Groovy/Grails Unit Test Error with custom codec

I am having an issue with the "Getting Started with Grails" tutorial from the Grails website. 我在Grails网站上遇到了“ Grails入门”教程的问题。 It is having me create a custom codec in the utils directory. 它让我在utils目录中创建自定义编解码器。 I have created the codec and it works in the application, however when I add the codec to my controller unit test, as the tutorial suggests, it fails. 我已经创建了编解码器,并且可以在应用程序中使用,但是,如本教程所述,当我将编解码器添加到控制器单元测试中时,它会失败。 Here is the message I get when I run "grails test-app UserController -unit": 这是我运行“ grails test-app UserController -unit”时收到的消息:

"No such property: SHACodec for class: racetrack.UserControllerTests" “没有此类属性:类:racetrack.UserControllerTests的SHACodec”

I have tried using the loadCodec() method to include the codec, but got the same message. 我尝试使用loadCodec()方法包括编解码器,但得到了相同的消息。

Does anyone have any suggestions on how to resolve this unit test issue? 是否有人对如何解决此单元测试问题有任何建议? If it's an import issue, what would the import path be for my SHACodec.groovy file if it is in the /grails-app/utils/? 如果是导入问题,如果我的SHACodec.groovy文件位于/ grails-app / utils /中,则导入路径为什么?

My tutorial code is available for download at http://arlitt.com/racetrack.zip . 我的教程代码可从http://arlitt.com/racetrack.zip下载。

I ran into this too (working through the code in the Grails book). 我也遇到了这个问题(通过Grails本书中的代码进行工作)。

What I found works is this: explicitly load the codec. 我发现有效的方法是:显式加载编解码器。 You don't need to include it in your imports. 您不需要将其包括在导入中。 Make sure that the SHACodec.groovy file is in the grails-app/utils directory. 确保SHACodec.groovy文件位于grails-app / utils目录中。

The following code snippet shows you how I did it. 以下代码段向您展示了我是如何做到的。

class UserControllerTests extends ControllerUnitTestCase{
    protected void setUp() {
        super.setUp()

        loadCodec (org.codehaus.groovy.grails.plugins.codecs.Base64Codec)
        loadCodec (racetrack.SHACodec)
    }
    // ...
}

Codecs are not automatically loaded, you need to load them. 编解码器不会自动加载,您需要加载它们。

Refer to this post for more details: http://kousenit.wordpress.com/2010/02/24/using-a-codec-in-a-grails-unit-test/ 请参阅此帖子以获取更多详细信息: http : //kousenit.wordpress.com/2010/02/24/using-a-codec-in-a-grails-unit-test/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM