我是單元/集成/功能測試的新手。我使用Spock在Grails 3.1.7中進行集成測試,並面臨一個問題:Grails項目中集成測試的Spock驗證問題
我有一個自定義的驗證器,用於啓動應用程序和grails控制檯,但不能在測試運行期間工作。
我有2個類似的驗證器爲2域類,其中一個通過,另一個 - 不。
此驗證的主要思想是用戶可能有2封電子郵件。一個用於登錄,另一個用於配置文件。用戶不能選擇其他用戶的電子郵件顯示在配置文件中(和相反)。
即通過相應的測試(施加在用戶域類到電子郵件)的一個:
validator: {email, user ->
UserProfile userProfile = UserProfile.findByProfileEmail(email)
if (!userProfile) {
true
} else if (userProfile?.user == user) {
true
} else {
false
}
}
另一個是相同的,但是用戶被檢查簡檔的電子郵件(在用戶配置域類施加到profileEmail):
validator: {profileEmail, userProfile ->
User user = User.findByEmail(profileEmail)
if (!user) {
true
} else if (user?.userProfile == userProfile){
true
} else {
false
}
}
從測試一個代碼示例:
def setup() {
userService.createUser(email: EMAIL, password: PASSWORD, firstName: FIRST_NAME)
springSecurityService.reauthenticate(EMAIL, PASSWORD)
}
void "Attempt to set non-unique profileEmail"() {
given: "Create additional user"
userService.createUser(email: EMAIL_ADDITIONAL, password: PASSWORD, firstName: FIRST_NAME)
when: "Set user's profile email the same as additional user's email"
userProfileService.updateUserProfile([profileEmail: EMAIL_ADDITIONAL])
then: "Check that non-unique profileEmail was not saved"
User.findByEmail(EMAIL).userProfile.profileEmail != EMAIL_ADDITIONAL
}
不知何故,我的自定義驗證器適用於控制檯和運行應用程序,但不適用於集成測試。
可能需要更多來自服務或域類的代碼示例?鏈接到github回購幫助?
你應該改掉你的問題分成較小的,以幫助那些閱讀。 – kazanaki
github回購肯定會幫助,如果你有它公開... – kazanaki
@kazanaki,謝謝你的建議。調整了這個問題,但無論如何我發現了一個答案。 –