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()
}
}

Grails里的集成测试代码试例的更多相关文章
- [iOS翻译]《iOS7 by Tutorials》在Xcode 5里使用单元測试(上)
简单介绍: 单元測试是软件开发的一个重要方面.毕竟,单元測试能够帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元測试不是万能的,但Apple把它作为开发工具包的 ...
- C++ 内存模型 write_x_read_y 试例构造
之前一段时间偶然在 B 站上刷到了南京大学蒋炎岩(jyy)老师在直播操作系统网课.点进直播间看了一下发现这个老师实力非凡,上课从不照本宣科,而且旁征博引又不吝于亲自动手演示,于是点了关注.后来开始看其 ...
- 33个超级有用必须要收藏的PHP代码样例
作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...
- java servlet 代码样例 (demo)
今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...
- paip.输入法编程--英文ati化By音标原理与中文atiEn处理流程 python 代码为例
paip.输入法编程--英文ati化By音标原理与中文atiEn处理流程 python 代码为例 #---目标 1. en vs enPHati 2.en vs enPhAtiSmp 3.cn vs ...
- 驱动里执行应用层代码之KeUserModeCallBack(WOW64是由三个动态库wow64.dll wow64win.dll wow64cpu.dll来实现)
在驱动层(ring0)里执行应用层(ring3)代码,这是个老生常谈的技术,而且方法也挺多. 这种技术的本质:其实就是想方设法在驱动层里把应用层代码弄到应用层去执行. 比如在APC异步调用中,KeIn ...
- 在Eclipse里设置格式化代码时不格式化注释
在Eclipse里设置格式化代码时不格式化注释 今天格式化代码 发现直接format会把注释也一块格式化了,有时候会把好好的注释弄的很乱.甚为头疼. 查阅之后解决办法如下: Windows -> ...
- 自己改写了一个图片局部放大的jquery插件页面里面的html代码少了,同一个页面可以调用多个
自己改写了一个图片局部放大的jquery插件页面里面的html代码少了,同一个页面可以调用多个,兼容ie8以上浏览器,别忘了引用jquery,我用的jquery/1.11.0/其他版本没有测试,另外需 ...
- thinkphp从数据库里的html代码显示页面不解析
首先,这个问题不应该出现在这里,因为以前在用ThinkPHP3.1.2的时候,利用富文本编辑器保存文本后,直接从数据库里面取出的数据都能正常显示,改用ThinkPHP3.2.3之后,thinkphp从 ...
随机推荐
- Flume Netcat Source
1.cd /usr/local2/flume/conf sudo vim netcat.conf # Name the components on this agent a1.sources = r1 ...
- 你真的懂SDWebImage?
SDWebImage已经到了用烂了的地步,对于一名优秀的开发者来说,会用只是最简单的一步,我们要能够研究到其底层的技术实现和设计思路原理.在网上偶然间看到了一篇文章,感觉不错,略作修改,批注,后面的内 ...
- 实现https
实现https 环境 1.三台主机分别为A,B,C. 2.A主机设置为CA和DNS服务器,ip为192.168.213.129 3.B主机为client,ip为192.168.213.253 4.C主 ...
- 这里有最全的C/C++入门到进阶书籍推荐,你需要嘛?
编程是操作性很强的一门知识,看书少不了,但只有学习和实践相结合才能起到很好的效果,一种学习方法是看视频->看书->研究书中例子->自己做些东西->交流->看书. 研究经典 ...
- Akka源码分析-Remote-位置透明
上一篇博客中,我们研究了remote模式下如何发消息给远程actor,其实无论如何,最终都是通过RemoteActorRef来发送消息的.另外官网也明确说明了,ActorRef是可以忽略网络位置的,这 ...
- Mac OS下配置 ADB环境变量
前提已经安装了Android sdk. 步骤打开终端Terminal, 输入open -e ~/.bash_profile, 若之前没有该文件,会自动创建.添加内容 export PATH=${PAT ...
- Horspool和BM算法解析
最近算法中学到了Horspool,KMP,BM三种算法.接下来给大家做个分享. Horspool算法: 算法思路: 1.分为匹配串,原串 2.从右往左依次匹配: 一旦遇到不匹配的,原串相对于匹配串 移 ...
- windows怎么进如debug调试
主要说一下64位Win7使用debug程序的方法 首先你要下载一个DOSBOX程序 这个程序是一个dos模拟器 这个程序的制作目的是运行经典的DOS游戏 -.- 下载地址:http://www.dos ...
- BZOJ 2946 SA/SAM
思路: 1. 二分+后缀数组 2.SAM //By SiriusRen #include <cstdio> #include <cstring> #include <al ...
- PHP富文本编辑器 之Kindeditor的使用 一
一.下载编辑器源码 KindEditor 4.1.10 (2013-11-23) [1143KB] 下载页面: http://kindeditor.net/down.php 二.部署编辑器 将下载文件 ...