Grails 2.0:测试 flash.message 时出现问题
我对我的 UserController
进行了单元测试,但自从升级到 Grails 2.0 以来,flash
变量似乎总是返回一个没有消息的空映射。
以下是 UserControllerTests
的一些代码片段:
@TestFor(UserController)
@Mock(User)
class UserControllerTests {
...
void testSaveSucceeds() {
params.userName = 'Joe'
...
controller.save()
assert null != flash.message
assert '/user/list' == response.redirectedUrl
}
}
在 UserController
中:
def save = {
def userInstance = new User(params)
if (userInstance.validate()) {
flash.message = message(code: 'default.created.message', args: [userInstance.userName ])
...
}
但我的测试结果如下:
assert null != flash.message
| | |
| [:] null
false
我也尝试过作为集成测试,因为否则 响应
也为空,但它没有解决闪存问题。 view
和 model
也存在同样的问题。
我缺少什么?非常感谢任何帮助。
问候
乔纳斯
编辑:
这是一个奇怪的场景:
我的控制器有以下内容:
def test = {
flash.message = "Message"
}
def save = {
flash.message = "Message"
}
我的测试看起来像这样:
void testSaveSucceeds() {
controller.save()
println ">>> ${flash}"
controller.test()
println ">>> ${flash}"
}
输出像这样:
>>> [:]
>>> [message:Message]
有趣的是,IntelliJ 中的调试器在测试中的断点处停止() 操作但不在 save() 中
,那怎么可能???
问候
乔纳斯
I have a unit test for my UserController
but since upgrading to Grails 2.0, the flash
variable always seems to return an emtpy map with no message.
Here are some code snippets of the UserControllerTests
:
@TestFor(UserController)
@Mock(User)
class UserControllerTests {
...
void testSaveSucceeds() {
params.userName = 'Joe'
...
controller.save()
assert null != flash.message
assert '/user/list' == response.redirectedUrl
}
}
In UserController
:
def save = {
def userInstance = new User(params)
if (userInstance.validate()) {
flash.message = message(code: 'default.created.message', args: [userInstance.userName ])
...
}
But my test result is as follows:
assert null != flash.message
| | |
| [:] null
false
I have tried as an integration test as well because otherwise the response
was null as weill but it did not fix the flash issue. The same problem also exists with view
and model
.
What am I missing? Any help highly appreciated.
Regards
Jonas
EDIT:
Here's a weird scenario:
My controller has the following:
def test = {
flash.message = "Message"
}
def save = {
flash.message = "Message"
}
My Test looks like that:
void testSaveSucceeds() {
controller.save()
println ">>> ${flash}"
controller.test()
println ">>> ${flash}"
}
The output like that:
>>> [:]
>>> [message:Message]
Interesting to mention is also that the debugger in IntelliJ stops at a breakpoint in the test() action but not in save()
HOW can that be????
Regards
Jonas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,这意味着
userInstance.validate()
返回false
即验证失败。For me it means that
userInstance.validate()
returnfalse
ie the validation failed.