Grails 独特的测试失败?
我正在尝试在 grails 中创建一个测试,以确保 unique:true 约束确实有效,这是我的类和测试文件:
package edu.drexel.goodwin.events.domain
class UpayConfig {
String name
String siteId
String postingCode
static constraints = {
name(blank:false, maxSize:50)
siteId(blank:false, unique:true)
postingCode(blank:false)
}
}
package edu.drexel.goodwin.events。域
导入 grails.test.*
类 UpayConfigTests 扩展 GrailsUnitTestCase { 受保护的无效设置(){ super.setUp() 但是
protected void tearDown() {
super.tearDown()
}
void testCleanUpayConfigValidates() {
mockForConstraintsTests UpayConfig
def cleanUpayConfig = create()
assertTrue cleanUpayConfig.validate()
}
void testUpayConfigSiteIdMustBeUnique() {
mockForConstraintsTests UpayConfig
def upayConfigOne = create()
def upayConfigTwo = create()
assertFalse upayConfigOne.validate()
assertFalse upayConfigTwo.validate()
upayConfigTwo.siteId = '81'
assertTrue upayConfigOne.validate()
assertTrue upayConfigTwo.validate()
}
UpayConfig create() {
def upayConfig = new UpayConfig(
siteId: '82',
name: 'SMT - Workshops',
postingCode: '6'
)
}
第二个测试失败了
,upayConfig 变量对于 .validate() 都返回 true,即使我告诉它们都有相同的 siteId...
我有一种感觉,这与以下事实有关:不是放在数据库中,只是存储在内存中吗?
非常感谢所有帮助,谢谢。 -阿萨夫
I am trying to create a test in grails to ensure indeed that the unique:true constraint works, here's my class and test file:
package edu.drexel.goodwin.events.domain
class UpayConfig {
String name
String siteId
String postingCode
static constraints = {
name(blank:false, maxSize:50)
siteId(blank:false, unique:true)
postingCode(blank:false)
}
}
package edu.drexel.goodwin.events.domain
import grails.test.*
class UpayConfigTests extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testCleanUpayConfigValidates() {
mockForConstraintsTests UpayConfig
def cleanUpayConfig = create()
assertTrue cleanUpayConfig.validate()
}
void testUpayConfigSiteIdMustBeUnique() {
mockForConstraintsTests UpayConfig
def upayConfigOne = create()
def upayConfigTwo = create()
assertFalse upayConfigOne.validate()
assertFalse upayConfigTwo.validate()
upayConfigTwo.siteId = '81'
assertTrue upayConfigOne.validate()
assertTrue upayConfigTwo.validate()
}
UpayConfig create() {
def upayConfig = new UpayConfig(
siteId: '82',
name: 'SMT - Workshops',
postingCode: '6'
)
}
}
But that second test fails, the upayConfig variables both return true for .validate() even though I am telling them both to have the same siteId...
I have a feeling this has something to do with the fact that these aren't being placed in the database, just being stored in memory?
All help is much appreciated, thank you.
-Asaf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一性将在数据库级别。您永远不会保存域名,因此就 upayConfigTwo 而言,它是唯一的。您需要进行常规模拟并实际调用 upayConfigOne 上的 save() 。
The uniqueness will be at the database level. You're never saving the domain, so as far as upayConfigTwo is concerned, it is unique. You'll need to do a regular mock and actually call save() on upayConfigOne.
谢谢。我查找了这个网站: http://www.ibm.com /developerworks/java/library/j-grails10209/index.html 它有一个名为“使用mockForConstraintsTests()测试唯一约束”的部分,因此在它之后我将我的测试修改为如下并且它正确通过:
感谢您的帮助,
-阿萨夫
Thank you. I looked up this website: http://www.ibm.com/developerworks/java/library/j-grails10209/index.html and it had a section called "Testing the unique constraint with mockForConstraintsTests()" so following it I modified my test to be as follows and it passed correctly:
Thank you for your help,
-Asaf