unit-testing grails tag

一个人想着一个人 提交于 2019-12-01 07:01:27

问题


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?


回答1:


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
}


来源:https://stackoverflow.com/questions/6176930/unit-testing-grails-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!