Grails 2.0:测试 flash.message 时出现问题

发布于 2024-12-17 15:56:53 字数 1524 浏览 0 评论 0原文

我对我的 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

我也尝试过作为集成测试,因为否则 响应 也为空,但它没有解决闪存问题。 viewmodel 也存在同样的问题。

我缺少什么?非常感谢任何帮助。

问候

乔纳斯

编辑:

这是一个奇怪的场景:

我的控制器有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-12-24 15:56:53

对我来说,这意味着 userInstance.validate() 返回 false 即验证失败。

For me it means that userInstance.validate() return false ie the validation failed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文