博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Grails里的集成测试代码试例
阅读量:6670 次
发布时间:2019-06-25

本文共 3679 字,大约阅读时间需要 12 分钟。

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

User.groovy

package com.grailsinactionclass 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.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: '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()        }        }

转载地址:http://xloxo.baihongyu.com/

你可能感兴趣的文章
as3随机数
查看>>
四种方案解决ScrollView嵌套ListView问题
查看>>
[IIS] IIS Framework "aspnet_regiis.exe" 注册
查看>>
乘法逆元模板
查看>>
Oracle更改redo log大小 or 增加redo log组
查看>>
matplotlib油漆基础
查看>>
SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial
查看>>
为什么要优化你的代码?
查看>>
各类小公式
查看>>
【杂文】2015年度总结
查看>>
25个可遇不可求的jQuery插件
查看>>
【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
查看>>
uiautomatorviewer 可以查看到android中的web 元素信息
查看>>
当Scheduler拿不到url的 时候,不能立即退出
查看>>
[每天一个知识点]34-职业生涯-用得着和用不着的知识
查看>>
操作系统 进程与线程 图解浅析
查看>>
coursera课程Text Retrieval and Search Engines之Week 2 Overview
查看>>
BZOJ 2768: [JLOI2010]冠军调查 最小割
查看>>
L - 辗转相除法(第二季水)
查看>>
Android自己定义控件(状态提示图表)
查看>>