Grails里的集成测试代码试例

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

User.groovy

package com.grailsinaction

class User {

    String loginId
    String password
    String homepage
    Date dateCreated

    static constraints = {
      loginId size: 3..20, unique: true, nullable: false
      
      password size: 6..8, nullable:false
      homepage url: true, nullable: true
    }
}

UserIntegrationSpec.groovy

package com.grailsinaction


import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*

@Integration
@Rollback
class 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: 'http://www.grailsinaction.com')
            when: "the user is saved"
            joe.save()
            
            then: "it saved successfully and can be found in the database"
            joe.errors.errorCount == 0
            joe.id != null
            User.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: 'http://www.grailsinaction.com')
            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: 'http://www.grailsinaction.com')
            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() == null
            assert chuck.hasErrors()
            
            when: "We fix the invalid properties"
            chuck.password = "fistfist"
            chuck.homepage = "http://www.chucknorrisfacts.com"
            chuck.validate()
            
            then: "The user saves and validates fine"
            !chuck.hasErrors()
            chuck.save()
        }
        
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值