我正在尝试为以 JSON 格式处理 REST 请求的控制器编写一些集成测试。我的控制器定义()创建是这样的:
class FooController {
...
def create() {
withFormat {
html {
[fooInstance: new Foo(params)]
}
json {
[fooInstance: new Foo(params.JSON)]
}
}
}
...
}
然后我有一个如下所示的集成测试:
@TestFor(FooController)
class FooControllerTests extends GroovyTestCase {
void testCreate() {
def controller = new FooController()
controller.request.contentType = "text/json"
// this line doesn't seem to actually do anything
controller.request.format = 'json'
// as of 2.x this seems to be necessary to get withFormat to respond properly
controller.response.format = 'json'
controller.request.content = '{"class" : "Foo", "value" : "12345"}'.getBytes()
def result = controller.create()
assert result
def fooIn = result.fooInstance
assert fooIn
assertEquals("12345", fooIn.value)
}
}
但 fooIn 始终为空。如果我调试测试,我可以看到调用 FooController.create() 时,params 也是空的。诚然,我不太了解集成测试应该如何在内部运行,但我希望看到代表我的 Foo 实例的数据。
有任何想法吗?