新的领域,多练练,这样写出的程序,确实坚固些。

也要理解集成测试与数据库相关,单元测试与类方法有关。

如果测试文件没有建立,按如下操作:

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写单元测试的更多相关文章

  1. 单元测试er——为什么真的真的要写单元测试

    优点 为什么很多技术或者知识要说优点?因为有些道理看着很简单,大家表面上都觉得对,但是做的时候又不去做或者做不到.其中有一个很重要原因是骨子里或者潜意识并没有真实觉得这是对的,一旦想去做的时候同时会冒 ...

  2. 为什么从前那些.NET开发者都不写单元测试呢?

    楔子 四年前我虽然也写了很多年代码,由于公司虽然规模不小,却并非一家规范化的软件公司,因此在项目中严格意义上来说并没有架构设计.也不写单元测试,后来有幸加入了一家公司,这家公司虽然也是一家小公司,但是 ...

  3. VSTS写单元测试

                          用VSTS写单元测试                           许多应用程序都会用到“用户”类型,今天我要用的是ConsoleApplicatio ...

  4. 如何为 Vue 项目写单元测试

    https://www.w3ctech.com/topic/2052 如何为 Vue 项目写单元测试 前端工程 明非 2017-07-18 4685 访问 1 分享 微信分享 译者:明非 链接:htt ...

  5. 为什么不针对internal接口写单元测试?

    测试驱动的开发(TDD,Test Driven Development)的核心理念,是要使得重构(refactoring)更为有效,而不是创建更多的测试. 对一个有着长生命周期的项目来讲,在它的第一个 ...

  6. 【快学springboot】在springboot中写单元测试[Happyjava]

    前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...

  7. 【快学springboot】在springboot中写单元测试

    前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...

  8. 如何用googletest写单元测试

    http://www.uml.org.cn/c++/201203293.asp googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows.linux.Mac等OS平台 ...

  9. 工作3年,还不会写单元测试?新技能get!

    历史遗留代码不敢重构? 每次改代码都要回归所有逻辑? 提测被打回? 在近期的代码重构的过程中,遇到了各式各样的问题.比如调整代码顺序导致bug,取反操作逻辑丢失,参数校验逻辑被误改等. 上线前需要花大 ...

随机推荐

  1. E20170621-hm

    detroit  底特律 giant   n. 巨人,大汉; 巨兽,巨物; 卓越人物 woo    vt. 求爱,求婚; 争取…的支持; convince   vt. 使相信,说服,使承认; 使明白; ...

  2. struts2基础学习----struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...

  3. ACM_小G的循环

    小G的循环 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一回生,二回熟,三回就腻,小G用for,while循环用得太多了,累觉 ...

  4. UNIX环境高级编程--7

    进程环境main函数:    C程序总是从main函数开始执行.main函数原型是:    int main(int argc, char *argv[]);    当内核执行C程序时(使用一个exe ...

  5. jq-文本框只能输入数字

    <input type="text" onKeyUp="value=value.replace(/\D/g,'')"  /> onKeyUp: 当输 ...

  6. AOP注解不起作用的debug结果

    经过2天的调试,我发现AOP注解配置不起作用居然是表达式的错误导致的 在xml文件中配置的base-package有关,初步认为@PointCut只能使用base-package..*(..)这样的方 ...

  7. C++ Primer(第4版)-学习笔记-第2部分:容器和算法

    第9章 顺序容器 顺序容器和关联容器 顺序容器内的元素按其位置存储和访问. 关联容器,其元素按键(key)排序. 顺序容器(sequential container). 顺序容器的元素排列次序与元素值 ...

  8. Server Tomcat v8.0 Server at localhost failed to start 问题解决方法?

    bi编程jsp  servlet 第一个程序: HelloServlet 运行错误 404: 十月 28, 2017 11:25:14 上午 org.apache.tomcat.util.digest ...

  9. jQuery——this

    js注册事件this代表的dom对象 jQuery注册事件this代表的也是dom对象,所以需要$(this)转成jQuery对象

  10. SQL server 2005中无法新建作用(Job)的问题

    1.在使用sqlserver2005创建作业时,创建不了,提示 无法将类型为“Microsoft.SqlServer.Management.Smo.SimpleObjectKey”的对象强制转换为类型 ...