2013-06-12 169 views
0

我正在嘗試對Grails域類進行基本的單元測試。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上述測試成功。我不明白爲什麼約束不會導致它失敗。

回答

2

您沒有編寫您使用的Grails版本,但通常應設置用於進行約束檢查的User類。添加到您的UserTests

def setUp() { 
    mockForConstraintsTests(User) 
} 
+0

我使用的Grails 2.2.1 – MeIr

+0

你能澄清並聖盃版本能起到什麼作用?不同的Grails版本有不同的單元測試定義嗎?謝謝。 – MeIr

+0

我加了你的方法,把「def」改爲「void」,但是測試仍然成功...... – MeIr