0

Okay, I have a custom taglib inside a custom taglib like so:

def preference = { attrs, body ->

        def sliderTaglib = grailsApplication.mainContext.getBean('com.myCustom.sliderTagLib')
        sliderTaglib.slider.call(attrs, body)

}

Since I am using grailsApplication.mainContext.getBean() , how do i mock that in my unit test? My test complains:

grails Error executing tag No bean named is defined...

I've tried various methods to mock it but to no avail. It functions correctly when I run-app though, it's just the test that fails. I'm using grails 2.3.9 and spock. :(

My test looks like this:

void "taglib should output a slider"() {
    when:
    def result = applyTemplate("""
        <a:preference class='slider'/>
    """)
    then:
    result == "<div class='slider'></div>"
}
  • 1
    how does your test look like? if you could provide some more details regarding the test, that might help – saw303 Jun 16 '14 at 11:48
  • I just used the regular `applyTemplate` and matched the result against expected. I'll update it above. – user3744677 Jun 16 '14 at 12:00

1 Answers1

0

You can mock the taglib by using

@TestMixin(GroovyPageUnitTestMixin)
class MultipleTagLibSpec extends Specification {

    void "test multiple tags"() {
        given:
        mockTagLib(sliderTagLib)

        expect:
        // …
    }
}

By the you should must not lookup the sliderTagLib bean in the application context. You simple call the taglib by calling the taglib-namespace.taglib-method.

E.g. the Grails taglibs are in the g namespace. Therefore use it like this.

def preference = { attrs, body ->
    out << g.link(...)
    // in your case
    yourNamespace.slider(....)
}
saw303
  • 8,051
  • 7
  • 50
  • 90
  • I see. I figured, since I can simply call g.message() or other grails standard taglibs in my taglib, theoretically I can just call my custom taglibs the same way... I shall try this first thing in the morning tomorrow. I think it was the first thing I tried, but I hit an error in the test, so I searched a bit on the internet, and ended up with getting the bean. – user3744677 Jun 16 '14 at 13:51
  • Thanks! It works! It just so happens that I didn't use out as in `out << myNamespace.slider` in the preference. :) – user3744677 Jun 17 '14 at 03:03