简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM