新的領域,多練練,這樣寫出的程序,確實堅固些。shell
也要理解集成測試與數據庫相關,單元測試與類方法有關。數據庫
若是測試文件沒有創建,按以下操做:post
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 } }