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从 ...
随机推荐
- PCB 挺有意思的基数排序----C#代码实现
今天在头条看一个很有意思的排序算法[基数排序],以前所学习的排序算法都是基于数值对比的方式排序的,而这个算法挺有意思的非常独特.但从网上看到的例子通常是对个位,十位处理,并转为对应的桶索引的方式实现, ...
- php获得文件的属性
PHP获取文件属性可以用到多种函数,来实现我们对文件各种不同信息的获取需求.在这里我们就简单的介绍了这些获取方式的实现方法. 详细解读PHP获取远程图片技巧 详细介绍PHP读取目录函数 如何运用相关函 ...
- JavaScript--什么是函数
函数是完成某个特定功能的一组语句.如没有函数,完成任务可能需要五行.十行.甚至更多的代码.这时我们就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义一 ...
- 手势识别官方教程(7)识别缩放手势用ScaleGestureDetector和SimpleOnScaleGestureListener
1.Use Touch to Perform Scaling As discussed in Detecting Common Gestures, GestureDetector helps you ...
- Java—RequestMapping相关用法
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.它有6个属性:1.value:指定请求的具体地址:valu ...
- java攻城狮之路--复习JDBC(利用BeanUtils、JDBC元数据编写通用的查询方法;元数据;Blob;事务;批量处理)
1.利用BeanUtils的前提得要加入以下两个jar包: commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar package com.shel ...
- Lazarus Reading XML- with TXMLDocument and TDOMNode
这里读取'HistoryPath' ,'TracePath' 元素下的‘value’属性使用的是 var xmlCfg: TXMLDocument; .... function ReadXMLCFG: ...
- linux最常用的快捷键
1.ctrl+alt+T 调出命令行界面 2.alt+f4 关闭当前窗口
- spark的体系结构
spark的体系结构 1.客户端(Driver Program) 需要构建一个对象,核心是sc(SparkContext) 以应用程序为例:链接本地 //new conf val conf=new S ...
- CAD得到自定义实体拖放夹点(com接口VB语言)
主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::getGripPoints 自定义实体事件,得到拖放夹点,详细说明如下: 参数 说明 LONGLO ...