Grails - 使用命名编组器的单元测试控制器

Grails - Unit test controller that's using a named marshaller

我正在尝试对使用命名编组器的控制器进行单元测试。控制器看起来像这样:

def userByEmail(UserByEmailCommand userByEmailCommand) {
    render checkForErrorsAndExecute(userByEmailCommand) { UserByEmailCommand cmd ->
        JSON.use("complete") {
            [users: [userService.getUserByPrimaryEmailAddress(cmd.email)]] as JSON
        }
    }
}

当我 运行 测试时,我收到一个异常 Converter Configuration with name 'complete' not found!

如有任何帮助,我们将不胜感激。谢谢

您可以在测试本身中创建命名配置。例如:

given: 'register marshaller'
JSON.createNamedConfig('complete') { 
    it.registerObjectMarshaller(Map) { Map item -> item }
}

when: 'controller action is called'
controller.userByEmail()

then:
// assertion goes here