【问题标题】:Grails Unit Test doesn't failGrails 单元测试不会失败
【发布时间】:2013-06-12 22:33:27
【问题描述】:

我正在尝试对 Grails 域类执行基本单元测试。

这是域类:

class User {
    String username
    String password
    String email

    static constraints = {
        username size: 4..15, blank: false, unique: true
        password size: 5..15, password: true, blank: false
        email email: true, blank: false
    }
}

这里是单元测试类:

@TestFor(User)
class UserTests {
    void testCreateUser() {
        def u = new User(username:"ab")
        assertFalse "There should be errors", u.validate()
        assertTrue "Should be errors here", u.hasErrors()
    }
}

username 的大小受 4 到 15 的限制。但是,当我运行 grails test-app 时,上述测试成功。我不明白为什么约束没有导致它失败。

【问题讨论】:

    标签: unit-testing grails


    【解决方案1】:

    你没有写你使用哪个 Grails 版本,但通常你应该设置 User 类来测试约束检查。将此添加到您的 UserTests

    def setUp() {
        mockForConstraintsTests(User)
    }
    

    【讨论】:

    • 你能澄清一下 Grail 版本有什么不同吗?不同的 Grails 版本是否有不同的单元测试定义?谢谢。
    • @MeIr 把mockForConstraintsTests(User) 行作为testCreateUser 的第一行,看看是否成功。
    猜你喜欢
    • 1970-01-01
    • 2014-02-14
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    相关资源
    最近更新 更多