用Grails写单元测试
新的领域,多练练,这样写出的程序,确实坚固些。
也要理解集成测试与数据库相关,单元测试与类方法有关。
如果测试文件没有建立,按如下操作:
Unit tests are generated automatically for any Grails artifacts you create using the
Grails shell commands ( grails create-controller for example). If you create an
artifact by hand in a text editor or by copy/paste, you can create a shell test using the
grails create-unit-test command.
还有,好像现在GRAILS自动生成的测试样码不太对,改成书上的就OK了。
package com.grailsinaction
import grails.test.mixin.TestMixin
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.*
/**
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
*/
@TestFor(PostController)
@Mock([User, Post])
class PostControllerSpec extends Specification {
def "Get a users timeline given their id"() {
given: "A user with posts in the db"
User chuck = new User(
loginId: "chuck_norris",
password: "password")
chuck.addToPosts(new Post(content: "A first post"))
chuck.addToPosts(new Post(content: "A second post"))
chuck.save(failOnError: true)
and: "A loginId parameter"
params.id = chuck.loginId
when: "the timeline is invoked"
def model = controller.timeline()
then: "the user is in the returned model"
model.user.loginId == "chuck_norris"
model.user.posts.size() == 2
}
def "Check that non-existent users are handled with an error"() {
given: "the id of a non-existent user"
params.id = "this-user-id-does-not-exist"
when: "the timeline is invoked"
controller.timeline()
then: "a 404 is sent to the browser"
response.status == 404
}
}

用Grails写单元测试的更多相关文章
- 单元测试er——为什么真的真的要写单元测试
优点 为什么很多技术或者知识要说优点?因为有些道理看着很简单,大家表面上都觉得对,但是做的时候又不去做或者做不到.其中有一个很重要原因是骨子里或者潜意识并没有真实觉得这是对的,一旦想去做的时候同时会冒 ...
- 为什么从前那些.NET开发者都不写单元测试呢?
楔子 四年前我虽然也写了很多年代码,由于公司虽然规模不小,却并非一家规范化的软件公司,因此在项目中严格意义上来说并没有架构设计.也不写单元测试,后来有幸加入了一家公司,这家公司虽然也是一家小公司,但是 ...
- VSTS写单元测试
用VSTS写单元测试 许多应用程序都会用到“用户”类型,今天我要用的是ConsoleApplicatio ...
- 如何为 Vue 项目写单元测试
https://www.w3ctech.com/topic/2052 如何为 Vue 项目写单元测试 前端工程 明非 2017-07-18 4685 访问 1 分享 微信分享 译者:明非 链接:htt ...
- 为什么不针对internal接口写单元测试?
测试驱动的开发(TDD,Test Driven Development)的核心理念,是要使得重构(refactoring)更为有效,而不是创建更多的测试. 对一个有着长生命周期的项目来讲,在它的第一个 ...
- 【快学springboot】在springboot中写单元测试[Happyjava]
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- 【快学springboot】在springboot中写单元测试
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- 如何用googletest写单元测试
http://www.uml.org.cn/c++/201203293.asp googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows.linux.Mac等OS平台 ...
- 工作3年,还不会写单元测试?新技能get!
历史遗留代码不敢重构? 每次改代码都要回归所有逻辑? 提测被打回? 在近期的代码重构的过程中,遇到了各式各样的问题.比如调整代码顺序导致bug,取反操作逻辑丢失,参数校验逻辑被误改等. 上线前需要花大 ...
随机推荐
- shell脚本-循环选择语句
shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...
- VUE使用QRcode或者vue-qr生成二维码
这里介绍两种vue生成二维码的方法 QRcode vue-qr vue-qr比QRcode功能多在可以在中间加logo 下面先介绍QRcode vue里安装qrcodejs的npm包 npm inst ...
- scrapy 简单操作
1.创建一个简单的scrapy项目 scrapy startproject search(项目名称)按照提示cd searchscrapy genspider serachname search.co ...
- zoj3675 BFS+状态压缩
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int n ...
- Ruby开发环境的搭建
1.Ruby的下载 https://rubyinstaller.org/downloads/ 2.Ruby的安装 3.Eclipse配置Ruby开发环境 插件地址:http://rubyeclipse ...
- C# 学习——静态(第四天)
一.命名空间 类似于文件夹,而类就是文件夹中的文件: 作用:明确的指向我们所需要的类的 所在的位置: 统一命名空间下,类名不能重复. 二.类 概念:具有相同属性和功能的对象的抽象的集合. 三.静态与实 ...
- Power Designer逆向操作(从mysql5.0生成数据库的物理模型)
Power Designer逆向操作(从mysql5.0生成数据库的物理模型) 环境:powderdesigner12.5:mysql5.0 步骤: 1. 为指定的数据库配置MySQL的ODBC数据 ...
- (转) Arcgis for js之WKT和GEOMETRY的相互转换
http://blog.csdn.net/gisshixisheng/article/details/44057453 1.wkt简介 WKT(Well-known text)是一种文本标记语言,用于 ...
- HDU_1023_Train Problem II_卡特兰数
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 结果缓冲区 resbuf具体用法
struct resbuf *rb; rb=acutBulidlist(RTSTR,text(), rtpoint,,,,,) 创建圆用法. acdbEntGet 返回结果缓冲区 然后 rb-> ...