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

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

问题描述

我写了一个 Grails 标签,它只是围绕 的一个非常薄的包装器Grails 选择标签

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: 否方法签名: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天全站免登陆