apache / apache/grails-core

Trouble Testing Tag That Interacts With pageScope

Open
#14,259 3 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-gsp
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

By @jeffbrown from https://github.com/grails/grails-core/issues/9576 where it was closed.

It still seems to be an issue
```
package demo

class DemoTagLib {
static namespace = 'demo'

def putSomethingInPageScope = { attrs ->
pageScope.someValue = attrs.foo
}
}
```
```
package demo

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(DemoTagLib)
class DemoTagLibSpec extends Specification {

void "use applyTemplate to test tag that puts something in pageScope"() {
when:
applyTemplate ''

then:
tagLib.pageScope.someValue == '42'
}

void "use tag as method to test tag that puts something in pageScope"() {
when:
tagLib.putSomethingInPageScope(foo: '42')

then:
tagLib.pageScope.someValue == '42'
}
}
```

The applyTemplate approach does not work. I think multiple pageScope (TemplateVariableBinding) objects are being created and the one populated by the tag is not the same one retrieved in the test.

Contributor guide

Open the contributing guide

Research direction

Start with the DemoTagLibSpec reproduction using applyTemplate and the tagLib.pageScope assertion, then compare it with the direct tagLib.putSomethingInPageScope case. Inspect how applyTemplate and TemplateVariableBinding handle pageScope; done means the applyTemplate test observes the value written by the tag, with the existing reproduction passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.