Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble Testing Tag That Interacts With pageScope #32

Open
sbglasius opened this issue Nov 1, 2018 · 3 comments
Open

Trouble Testing Tag That Interacts With pageScope #32

sbglasius opened this issue Nov 1, 2018 · 3 comments

Comments

@sbglasius
Copy link
Contributor

By @jeffbrown from grails/grails-core#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 '<demo:putSomethingInPageScope foo="42"/>'

        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.

@sbglasius
Copy link
Contributor Author

Will this be looked at?

@sbglasius
Copy link
Contributor Author

Ran into this again today.

@sbglasius
Copy link
Contributor Author

@puneetbehl / @jeffbrown This is still a very frustrating problem. It's possible to write well-functional taglibs, but impossible to write a good test for them.

As far as I can tell, there are produced two page-bindings, one by org.grails.web.taglib.encoder.WebOutputContextLookup.WebOutputContext#createAndRegisterRootBinding and one by org.grails.gsp.GroovyPageWritable#doWriteTo (using the parent binding). It seems that the second created is the one used by the tag-lib, when accessing pageScope but for some reason, the applied template cannot find the variables written to it.

It looks like the method in org.grails.taglib.TemplateVariableBinding#getVariable is actually the one getting called by the tag-library, not org.grails.gsp.GroovyPageBinding#getVariable, but I have not been able to confirm this, and the reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant