11<%=packageName ? "package ${ packageName} " : ''%>
22
3+ import spock.lang.*
4+
35import grails.testing.gorm.DomainUnitTest
46import grails.testing.web.controllers.ControllerUnitTest
5- import spock.lang.*
67
78class ${ className} ControllerSpec extends Specification implements ControllerUnitTest<${ className} Controller>, DomainUnitTest<${ className} > {
89
910 def populateValidParams(params) {
1011 assert params != null
11- assert false , " TODO: Populate valid params"
12+ assert false , ' TODO: Populate valid params'
1213 }
1314
1415 def populateInvalidParams(params) {
1516 assert params != null
16- assert false , " TODO: Populate invalid params"
17+ assert false , ' TODO: Populate invalid params'
1718 }
1819
19- void "Test the index action returns the correct model"() {
20+ void 'Test the index action returns the correct model'() {
21+ when: ' The index action is executed'
22+ controller.index().get()
2023
21- when:" The index action is executed"
22- controller.index().get()
23-
24- then:" The model is correct"
25- ! model.${modelName} List
26- model.${ modelName} Count == 0
24+ then: ' The model is correct'
25+ ! model.${modelName} List
26+ model.${ modelName} Count == 0
2727 }
2828
29- void " Test the create action returns the correct model" () {
30- when:" The create action is executed"
31- controller.create()
29+ void ' Test the create action returns the correct model' () {
30+ when: ' The create action is executed'
31+ controller.create()
3232
33- then:" The model is correctly created"
34- model.${modelName} != null
33+ then: ' The model is correctly created'
34+ model.${modelName} != null
3535 }
3636
37- void "Test the save action correctly persists an instance"() {
38-
39- when:" The save action is executed with an invalid instance"
40- request.method = " POST"
41- def ${propertyName} = new ${ className} ()
42- ${ propertyName} .validate()
43- controller.save(${ propertyName} ).get()
37+ void 'Test the save action correctly persists an instance'() {
38+ when: ' The save action is executed with an invalid instance'
39+ request.method = ' POST'
40+ def ${propertyName} = new ${ className} ()
41+ ${ propertyName} .validate()
42+ controller.save(${ propertyName} ).get()
4443
45- then:" The create view is rendered again with the correct model"
46- model.${ modelName} != null
47- view == 'create'
44+ then: ' The create view is rendered again with the correct model'
45+ model.${ modelName} != null
46+ view == 'create'
4847
49- when:" The save action is executed with a valid instance"
50- response.reset()
51- populateValidParams(params)
52- ${ propertyName} = new ${ className} (params)
48+ when: ' The save action is executed with a valid instance'
49+ response.reset()
50+ populateValidParams(params)
51+ ${ propertyName} = new ${ className} (params)
5352
54- controller.save(${ propertyName} ).get()
53+ controller.save(${ propertyName} ).get()
5554
56- then:" A redirect is issued to the show action"
57- response.status == 201
58- ${ className} .count() == 1
55+ then: ' A redirect is issued to the show action'
56+ response.status == 201
57+ ${ className} .count() == 1
5958 }
6059
61- void " Test that the show action returns the correct model" () {
62- when:" The show action is execu ted with a null domain"
63- controller.show(null).get()
60+ void ' Test that the show action returns the correct model' () {
61+ when: ' The show action is execu ted with a null domain'
62+ controller.show(null).get()
6463
65- then:" A 404 error is returned"
66- response.status == 404
64+ then: ' A 404 error is returned'
65+ response.status == 404
6766
68- when:" A domain instance is passed to the show action"
69- populateValidParams(params)
70- def ${propertyName} = new ${ className} (params).save(flush:true)
67+ when: ' A domain instance is passed to the show action'
68+ populateValidParams(params)
69+ def ${propertyName} = new ${ className} (params).save(flush:true)
7170
72- controller.show(${ propertyName} .id).get()
71+ controller.show(${ propertyName} .id).get()
7372
74- then:" A model is populated containing the domain instance"
75- model.${ modelName} .id==${ propertyName} .id
73+ then: ' A model is populated containing the domain instance'
74+ model.${ modelName} .id==${ propertyName} .id
7675 }
7776
78- void " Test that the edit action returns the correct model" () {
79- when:" The edit action is executed with a null domain"
80- controller.edit(null).get()
77+ void ' Test that the edit action returns the correct model' () {
78+ when: ' The edit action is executed with a null domain'
79+ controller.edit(null).get()
8180
82- then:" A 404 error is returned"
83- response.status == 404
81+ then: ' A 404 error is returned'
82+ response.status == 404
8483
85- when:" A domain instance is passed to the edit action"
86- populateValidParams(params)
87- def ${propertyName} = new ${ className} (params).save(flush:true)
88- controller.edit(${ propertyName} ?.id).get()
84+ when: ' A domain instance is passed to the edit action'
85+ populateValidParams(params)
86+ def ${propertyName} = new ${ className} (params).save(flush:true)
87+ controller.edit(${ propertyName} ?.id).get()
8988
90- then:" A model is populated containing the domain instance"
91- model.${ modelName} .id==${ propertyName} .id
89+ then: ' A model is populated containing the domain instance'
90+ model.${ modelName} .id==${ propertyName} .id
9291 }
9392
94- void " Test the update action performs an update on a valid domain instance" () {
95- when:" Update is called for a domain instance that doesn't exist"
96- request.method = " PUT"
97- controller.update(null).get()
98-
99- then:" A 404 error is returned"
100- status == 404
101-
102- when:" An invalid domain instance is passed to the update action"
103- response.reset()
104- populateValidParams(params)
105- def ${propertyName} = new ${ className} (params).save(flush:true)
106- params.clear()
107- populateInvalidParams(params)
108- controller.update(${ propertyName} .id).get()
109-
110- then:" The edit view is rendered again with the invalid instance"
111- view == 'edit'
112- model.${ modelName} .id==${ propertyName} .id
113-
114- when:" A valid domain instance is passed to the update action"
115- response.reset()
116- params.clear()
117- populateValidParams(params)
118- controller.update(${ propertyName} .id).get()
119-
120- then:" A redirect is issued to the show action"
121- ${ propertyName} != null
122- response.status == 200
123- !book.isDirty()
93+ void ' Test the update action performs an update on a valid domain instance' () {
94+ when: " Update is called for a domain instance that doesn't exist"
95+ request.method = ' PUT'
96+ controller.update(null).get()
97+
98+ then: ' A 404 error is returned'
99+ status == 404
100+
101+ when: ' An invalid domain instance is passed to the update action'
102+ response.reset()
103+ populateValidParams(params)
104+ def ${propertyName} = new ${ className} (params).save(flush:true)
105+ params.clear()
106+ populateInvalidParams(params)
107+ controller.update(${ propertyName} .id).get()
108+
109+ then: ' The edit view is rendered again with the invalid instance'
110+ view == 'edit'
111+ model.${ modelName} .id==${ propertyName} .id
112+
113+ when: ' A valid domain instance is passed to the update action'
114+ response.reset()
115+ params.clear()
116+ populateValidParams(params)
117+ controller.update(${ propertyName} .id).get()
118+
119+ then: ' A redirect is issued to the show action'
120+ ${ propertyName} != null
121+ response.status == 200
122+ !book.isDirty()
124123 }
125124
126- void " Test that the delete action deletes an instance if it exists" () {
127- when:" The delete action is called for a null instance"
128- request.method = " DELETE"
129- controller.delete(null).get()
125+ void ' Test that the delete action deletes an instance if it exists' () {
126+ when: ' The delete action is called for a null instance'
127+ request.method = ' DELETE'
128+ controller.delete(null).get()
130129
131- then:" A 404 is returned"
132- status == 404
130+ then: ' A 404 is returned'
131+ status == 404
133132
134- when:" A domain instance is created"
135- response.reset()
136- populateValidParams(params)
137- def ${propertyName} = new ${ className} (params).save(flush: true)
133+ when: ' A domain instance is created'
134+ response.reset()
135+ populateValidParams(params)
136+ def ${propertyName} = new ${ className} (params).save(flush: true)
138137
139- then:" It exists"
140- ${ className} .count() == 1
138+ then: ' It exists'
139+ ${ className} .count() == 1
141140
142- when:" The domain instance is passed to the delete action"
143- controller.delete(${ propertyName} .id).get()
141+ when: ' The domain instance is passed to the delete action'
142+ controller.delete(${ propertyName} .id).get()
144143
145- then:" The instance is deleted"
146- ${ className} .count() == 0
147- response.status == 204
144+ then: ' The instance is deleted'
145+ ${ className} .count() == 0
146+ response.status == 204
148147 }
149- }
148+
149+ }
0 commit comments