Testing 测试grailstaglib
Grails1.1。 我的自定义标记:Testing 测试grailstaglib,testing,grails,taglib,Testing,Grails,Taglib,Grails1.1。 我的自定义标记: class MyTagLib { static namespace 'ct' def textField = {attrs -> def bean = attrs.remove('bean') def field = attrs.remove('field') attrs.name = field out << render(template:"/templates/textField", mode
class MyTagLib {
static namespace 'ct'
def textField = {attrs ->
def bean = attrs.remove('bean')
def field = attrs.remove('field')
attrs.name = field
out << render(template:"/templates/textField", model:[
required: !bean.constraints[field].nullable,
display : bean["${bean.trainingExperience.type}"][field],
theTag : g.textField(name : field, value : bean[field]),
value : bean[field]
])
}
是否可以测试在模型中使用正确的值呈现正确的模板
MyTagLibTests
public class CareertracTagLibTests extends TagLibUnitTestCase{
protected void setUp() {
super.setUp()
mockTagLib(FormTagLib)
mockTagLib(RenderTagLib)
def g = new FormTagLib() // interpret "g" namespace as instances of FormTagLib
tagLib.metaClass.g = g
String.metaClass.encodeAsHTML = {org.codehaus.groovy.grails.plugins.codecs.HTMLCodec.encode(it)}
}
void TestTextField() {
tagLib.textField([bean : mockBean, field : 'viewField'])
def x = new RenderTagLib().render(template:"/templates/textField",
model:[required:false,
display:"view",
// Snip
])
assertEquals tagLib.out, x.out // Or something like this
}
}
}使用
TagLibUnitTestCase
可以使用renderArgs
测试对render方法的调用,方法与在ControllerUnitTestCase
中相同。renderArgs
属性只是一个映射,用于存储上次调用render
动态方法的参数。因此,在您的示例中,您可能需要以下内容:
assertEquals "/templates/textField", renderArgs.template
assertFalse renderArgs.model.required
等等。我必须使用tagLib.renderags来实现这一点。(Grails 1.3.7)
assertEquals "/templates/textField", renderArgs.template
assertFalse renderArgs.model.required