无论如何,在 grails 控制器中模拟命令对象都会导致 hasErrors() 返回 false !请帮忙

发布于 2024-09-03 07:17:35 字数 1159 浏览 1 评论 0原文

我有一个在控制器操作中使用命令对象的控制器。当在 grails 控制器单元测试中模拟此命令对象时,hasErrors() 方法始终返回 false,即使我故意违反其约束也是如此。更令人困惑的是,在生产中,hasErrors() 有效!所以这只是一个测试问题。

 def save = { RegistrationForm form ->
  if(form.hasErrors()) {
   // code block never gets executed
  } else {
   // code block always gets executed
  }
 }

在测试本身中,我这样做:

 mockCommandObject(RegistrationForm)
 def form = new RegistrationForm(emailAddress: "ken.bad@gmail",
  password: "secret", confirmPassword: "wrong")

 controller.save(form)

我故意给它一个错误的电子邮件地址,并确保密码和确认密码属性不同。在这种情况下,hasErrors() 应该返回 true...但事实并非如此。如果这样的基本事情不起作用,我不知道我的测试如何可靠:/

这是 RegistrationForm 类,因此您可以看到我正在使用的约束:

class RegistrationForm {
 def springSecurityService

 String emailAddress
 String password
 String confirmPassword

 String getEncryptedPassword() {
  springSecurityService.encodePassword(password)
 }

 static constraints = {
  emailAddress(blank: false, email: true)
  password(blank: false, minSize:4, maxSize: 10)
  confirmPassword(blank: false, validator: { confirmPassword, form ->
   confirmPassword == form.password
  })
 }
}

I have a controller that uses a command object in a controller action. When mocking this command object in a grails' controller unit test, the hasErrors() method always returns false, even when I am purposefully violating its constraints. The more baffling thing is that in production, hasErrors() works! So this is just a testing problem.

 def save = { RegistrationForm form ->
  if(form.hasErrors()) {
   // code block never gets executed
  } else {
   // code block always gets executed
  }
 }

In the test itself, I do this:

 mockCommandObject(RegistrationForm)
 def form = new RegistrationForm(emailAddress: "ken.bad@gmail",
  password: "secret", confirmPassword: "wrong")

 controller.save(form)

I am purposefully giving it a bad email address, and I am making sure the password and the confirmPassword properties are different. In this case, hasErrors() should return true... but it doesn't. I don't know how my testing can be any where reliable if such a basic thing does not work :/

Here is the RegistrationForm class, so you can see the constraints I am using:

class RegistrationForm {
 def springSecurityService

 String emailAddress
 String password
 String confirmPassword

 String getEncryptedPassword() {
  springSecurityService.encodePassword(password)
 }

 static constraints = {
  emailAddress(blank: false, email: true)
  password(blank: false, minSize:4, maxSize: 10)
  confirmPassword(blank: false, validator: { confirmPassword, form ->
   confirmPassword == form.password
  })
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

四叶草在未来唯美盛开 2024-09-10 07:17:35

您尝试过mockForConstraintsTests吗?

例如,

void testSomething() {

    mockForConstraintsTests(RegistrationForm)

    def form = new RegistrationForm(emailAddress: "ken.bad@gmail", password: "secret", confirmPassword: "wrong")

    form.validate()

    assert 1 == form.errors.getErrorCount()
}

尝试先在其自己的单元测试中测试 RegistrationForm 命令对象。 (获得一些对其实际工作的信心)

也许使用上述内容作为测试的基础会有所帮助!?!?

Have you tried mockForConstraintsTests ?

E.g. something like...

void testSomething() {

    mockForConstraintsTests(RegistrationForm)

    def form = new RegistrationForm(emailAddress: "ken.bad@gmail", password: "secret", confirmPassword: "wrong")

    form.validate()

    assert 1 == form.errors.getErrorCount()
}

Try just testing the RegistrationForm command object first in its own unit test. (gain some confidence that its actually working)

Maybe using the above for the basis of your test will help!?!?

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