在控制器单元测试中存根自定义 TagLib 方法
我在自定义标签库中有一个方法,如下所示:
def deleteAction = {attrs ->
def id = attrs['id']
def type = attrs['type']
def clazz = attrs['class']
def html = new MarkupBuilder(out)
html.span(class: "${clazz} ui-icon ui-icon-trash {id:'${id}'}")
}
我有一个使用此方法的控制器,我试图将其存根以进行单元测试,所以我有以下内容:
def mockMyTagLib = mockFor(MyTagLib)
mockMyTagLib.demand.deleteAction(1) {id, type, clazz ->
def html = new MarkupBuilder(new StringWriter())
html.span(class: "${clazz} ui-icon ui-icon-trash {id:'${id}'}")
}
controller.metaClass.mn = mockMyTagLib.createMock()
但我不断收到以下内容:
不再调用“deleteAction” 预计此时。结束于 要求。
我在这里做错了什么吗?这是它在控制器中的实际用法:
"${mn.deleteAction(id: it.id, type: 'bookProduct', 'class': 'del-book-product')}"
I have a method in a custom taglib like so:
def deleteAction = {attrs ->
def id = attrs['id']
def type = attrs['type']
def clazz = attrs['class']
def html = new MarkupBuilder(out)
html.span(class: "${clazz} ui-icon ui-icon-trash {id:'${id}'}")
}
I have a controller that uses this method and I'm trying to stub it out for a unit test, so I have the following:
def mockMyTagLib = mockFor(MyTagLib)
mockMyTagLib.demand.deleteAction(1) {id, type, clazz ->
def html = new MarkupBuilder(new StringWriter())
html.span(class: "${clazz} ui-icon ui-icon-trash {id:'${id}'}")
}
controller.metaClass.mn = mockMyTagLib.createMock()
But I keep getting the following:
No more calls to 'deleteAction'
expected at this point. End of
demands.
Am I doing something wrong here? Here is it's actual usage in the controller:
"${mn.deleteAction(id: it.id, type: 'bookProduct', 'class': 'del-book-product')}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容来自测试 - 参考文档
您已指定demand.deleteAction(1),这意味着该方法必须调用一次且仅调用一次。
另外,如果您愿意,您可以随时将模拟设置为宽松,方法是将其指定为mockFor中的第二个参数(默认为严格)
The following is from Testing - Reference Documentation
You've specified demand.deleteAction(1) which means that the method must be called once and only once.
Also, if you want, you can always set your mock to be loose by specifying it as the second parameter in mockFor (defaults to strict)