700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Grails里的集成测试代码试例

Grails里的集成测试代码试例

时间:2023-10-17 03:14:34

相关推荐

Grails里的集成测试代码试例

测试的命令,3和2不一样了,要找找。。

User.groovy

package com.grailsinactionclass User {String loginIdString passwordString homepageDate dateCreatedstatic constraints = {loginId size: 3..20, unique: true, nullable: falsepassword size: 6..8, nullable:falsehomepage url: true, nullable: true}}

UserIntegrationSpec.groovy

package com.grailsinactionimport grails.test.mixin.integration.Integrationimport grails.transaction.*import spock.lang.*@Integration@Rollbackclass UserIntegrationSpec extends Specification {def setup() {}def cleanup() {}def "Saving our first user to the database"() {given: "A brand new user"def joe = new User(loginId: 'Joe', password: 'secret',homepage: '')when: "the user is saved"joe.save()then: "it saved successfully and can be found in the database"joe.errors.errorCount == 0joe.id != nullUser.get(joe.id).loginId == joe.loginId}def "Updating a saved user changes its properties"() {given: "An existing user"def existingUser = new User(loginId: 'Joe', password: 'secret',homepage: '')existingUser.save(failOnError: true)when: "A property is changed"def foundUser = User.get(existingUser.id)foundUser.password = 'sesame'foundUser.save(failOnError: true)then: "The change is reflected in the database"User.get(existingUser.id).password == 'sesame'}def "Deleting an existing user removes it from the database"() {given: "An existing user"def user = new User(loginId: 'Joe', password: 'secret',homepage: '')user.save(failOnError: true)when: "The user is deleted"def foundUser = User.get(user.id)foundUser.delete(flush: true)then: "The user is removed from the database"!User.exists(foundUser.id)}def "Saving a user with invalid properties causes an error"() {given: "A user which fails several field validations"def user = new User(loginId: 'Joe', password: 'tiny',homepage: 'not-a-url')when: "The user is validated"user.validate()then:user.hasErrors()"size.toosmall" == user.errors.getFieldError("password").code"tiny" == user.errors.getFieldError("password").rejectedValue"url.invalid" == user.errors.getFieldError("homepage").code"not-a-url" == user.errors.getFieldError("homepage").rejectedValue!user.errors.getFieldError("loginId")}def "Recovering from a failed save by fixing invalid properties"() {given: "A user that has invalid properties"def chuck = new User(loginId: 'chuck', password: 'tiny',homepage: 'not-a-url')assert chuck.save() == nullassert chuck.hasErrors()when: "We fix the invalid properties"chuck.password = "fistfist"chuck.homepage = ""chuck.validate()then: "The user saves and validates fine"!chuck.hasErrors()chuck.save()}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。