Dónal
Dónal

Reputation: 187529

unit-testing grails tag

I've written a Grails tag that is just a very thin wrapper around the Grails select tag

package com.example

class MyTagLib {
  def listTrees = {attrs ->
    List<TreeDto> allTrees = getMandatoryAttributeValue(attrs, 'trees')
    out << g.select(from: allTrees)
  }
}

I've wrtitten a unit test for this class, but when I run it, I get the following error when the last line is executed:

groovy.lang.MissingMethodException: No signature of method: com.example.MyTagLib.select() is applicable for argument types: (java.util.LinkedHashMap)

It seems like the reference to the grails tags in the g namespace are not available when running unit tests. I've tried creating an integration test instead, but this doesn't work either.

Is there a way to test test a tag that calls another tag without stubbing/mocking the output of that other tag?

Upvotes: 3

Views: 802

Answers (1)

Gregg
Gregg

Reputation: 35864

You have to mock the grails taglib you are using and inject it via the metaClass mechanism.

protected void setUp() {
    super.setUp()
    mockTagLib(FormTagLib)
    def g = new FormTagLib()
    tagLib.metaClass.g = g
}

Upvotes: 7

Related Questions