0

我正在尝试对 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上述测试成功时。我不明白为什么约束没有导致它失败。

4

1 回答 1

2

您没有编写您使用的 Grails 版本,但通常您应该设置要测试的 User 类以进行约束检查。将此添加到您的 UserTests

def setUp() {
    mockForConstraintsTests(User)
}
于 2013-06-12T14:13:35.140 回答