ruthst00 commented on code in PR #16453:
URL: https://github.com/apache/grails-core/pull/16453#discussion_r4158909637
##########
grails-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy:
##########
@@ -535,6 +537,52 @@ class InterceptorSpec extends Specification {
'/grails/foo/x' | true
}
+ void "Test getModel returns null when neither MODEL_AND_VIEW nor
TEMPLATE_MODEL is set"() {
+ given: "an interceptor with no model attributes on the request"
+ def i = new TestInterceptor()
+ GrailsWebMockUtil.bindMockWebRequest()
+
+ expect: "getModel returns null"
+ i.model == null
+ }
+
+ void "Test getModel returns modelMap from ModelAndView when MODEL_AND_VIEW
is set"() {
+ given: "an interceptor with a ModelAndView on the request"
+ def i = new TestInterceptor()
+ def webRequest = GrailsWebMockUtil.bindMockWebRequest()
+ def mav = new ModelAndView()
+ mav.addObject('foo', 'bar')
+ webRequest.setAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW,
mav, 0)
+
+ expect: "getModel returns the ModelAndView's model map"
+ i.model == [foo: 'bar']
+ }
+
+ void "Test getModel falls back to TEMPLATE_MODEL when no ModelAndView is
set"() {
+ given: "an interceptor with only TEMPLATE_MODEL on the request"
+ def i = new TestInterceptor()
+ def webRequest = GrailsWebMockUtil.bindMockWebRequest()
+ Map<String, Object> templateModel = [baz: 'qux']
+ webRequest.setAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL,
templateModel, 0)
Review Comment:
Four files were changed in `grails-test-examples/views-functional-tests`:
1. **`RenderTemplateController.groovy`** (new) — a minimal controller whose
`index()` action calls `render(template: 'snippet', model: [message: 'hello
from template'])`. This is the exact public flow that stores the model in
`GrailsApplicationAttributes.TEMPLATE_MODEL` rather than in a `ModelAndView`,
exercising the fallback branch of `Interceptor#getModel()` that the existing
unit tests only covered by directly setting the request attribute.
2. **`grails-app/views/renderTemplate/_snippet.gsp`** (new) — the GSP
partial template rendered by the controller, containing `${message}` so the
render call succeeds end-to-end.
3. **`ModelInterceptor.groovy`** — extended the controller regex from
`'modelAndView|respond|return'` to
`'modelAndView|respond|return|renderTemplate'` so the interceptor fires for the
new controller.
4. **`ModelInterceptorIntSpec.groovy`** — added a new feature method
`"interceptor after() can read model set by render(template:..., model:...) via
TEMPLATE_MODEL fallback"` that hits `/renderTemplate`, asserts HTTP 200, and
then asserts that `modelInterceptor.latestModel` is non-null, is a `Map`, and
contains `message == 'hello from template'` — verifying the actual model
contents rather than just presence.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]