Grails里的集成测试代码试例

简介: 测试的命令,3和2不一样了,要找找。。 User.groovy package com.grailsinaction class User { String loginId String password String homepage Da...

测试的命令,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()
        }
        
}

目录
相关文章
|
30天前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
2月前
|
测试技术
包含用例执行时间的测试报告代码
包含用例执行时间的测试报告代码
|
4月前
com串口通信测试代码
com串口通信测试代码
28 0
|
4月前
|
存储 人工智能 C#
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
36 0
|
15天前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出"验证成功",否则输出"验证失败"。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
1月前
|
JavaScript Go 项目管理
云效常见问题之使用gitlab仓库将代码合并评审环节集成到云效如何解决
云效(CloudEfficiency)是阿里云提供的一套软件研发效能平台,旨在通过工程效能、项目管理、质量保障等工具与服务,帮助企业提高软件研发的效率和质量。本合集是云效使用中可能遇到的一些常见问题及其答案的汇总。
97 1
|
1月前
|
测试技术 数据库 Python
python测试代码(二)
python测试代码(二)
19 0
|
1月前
|
Java 测试技术
单元测试编写可测试代码
单元测试编写可测试代码
19 2
|
2月前
|
存储 测试技术
统计测试结果的代码实现接昨天
统计测试结果的代码实现接昨天
|
3月前
|
网络协议 Linux C语言
C语言-多播测试代码(IPv4和IPv6)
C语言-多播测试代码(IPv4和IPv6)
35 0

热门文章

最新文章