无论如何,在 grails 控制器中模拟命令对象都会导致 hasErrors() 返回 false !请帮忙
我有一个在控制器操作中使用命令对象的控制器。当在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过mockForConstraintsTests吗?
例如,
尝试先在其自己的单元测试中测试
RegistrationForm
命令对象。 (获得一些对其实际工作的信心)也许使用上述内容作为测试的基础会有所帮助!?!?
Have you tried
mockForConstraintsTests
?E.g. something like...
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!?!?