开发者

Stubbing Custom TagLib method in Controller Unit Test

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 Mark开发者_StackOverflowupBuilder(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')}"


The following is from Testing - Reference Documentation

... You then specify the name of the method that you want to mock with an optional range as its argument. This range determines how many times you expect the method to be called, so if the number of invocations falls outside of that range (either too few or too many) then an assertion error will be thrown. If no range is specified, a default of "1..1" is assumed, i.e. that the method must be called exactly once.

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)

mockFor(class, loose = false)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜