Grails 独特的测试失败?

发布于 2024-10-30 05:35:32 字数 1441 浏览 4 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(2

硪扪都還晓 2024-11-06 05:35:32

唯一性将在数据库级别。您永远不会保存域名,因此就 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.

南汐寒笙箫 2024-11-06 05:35:32

谢谢。我查找了这个网站: http://www.ibm.com /developerworks/java/library/j-grails10209/index.html 它有一个名为“使用mockForConstraintsTests()测试唯一约束”的部分,因此在它之后我将我的测试修改为如下并且它正确通过:

void testUpayConfigSiteIdMustBeUnique() {
    def upayConfigOne = create()
    mockForConstraintsTests(UpayConfig, [upayConfigOne])
    def upayConfigTwo = create()
    assertFalse upayConfigTwo.validate()
    assertEquals "unique", upayConfigTwo.errors["siteId"]

    upayConfigTwo.siteId = '81'
    assertTrue upayConfigTwo.validate()
}

感谢您的帮助,
-阿萨夫

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:

void testUpayConfigSiteIdMustBeUnique() {
    def upayConfigOne = create()
    mockForConstraintsTests(UpayConfig, [upayConfigOne])
    def upayConfigTwo = create()
    assertFalse upayConfigTwo.validate()
    assertEquals "unique", upayConfigTwo.errors["siteId"]

    upayConfigTwo.siteId = '81'
    assertTrue upayConfigTwo.validate()
}

Thank you for your help,
-Asaf

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