单元测试grails标记 [英] unit-testing grails tag

查看:150
本文介绍了单元测试grails标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个Grails标签,它只是一个非常薄的包装器,围绕着 Grails select 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)
  }
}

我已经wrtitten这个类的单元测试,但是当我运行它时,在执行最后一行时出现以下错误:

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:


常规。 lang.MissingMethodException:无
方法签名:
com.example.MyTagLib.select()是
适用于参数类型:
(java.util.LinkedHashMap)

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

看起来像 g 命名空间中对grails标记的引用在运行单元测试。我已经尝试创建一个集成测试,但这也不起作用。

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?

推荐答案

你必须模拟你正在使用的grails taglib,并通过metaClass机制注入它。

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
}

这篇关于单元测试grails标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆