Grails单元测试验证模拟方法称为 [英] Grails unit test verify mock method called

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

问题描述

在单元测试中,我模拟了一个服务(这是被测类的引用).

赞:

given:
def mockXxService = mockFor(XxService)
mockXxService.demand.xxx(1) {->}
service.xxService = mockXxService
when:
service.yyy()
then:
// verify mockXxService's xxx method is invoked.

对于我的单元测试,我想验证是否调用了mockXxService.xxx().但是grails文档的mockControl.verify()对我不起作用.不知道如何正确使用它.

它与Mockito的verify方法非常相似.

有人知道吗?

解决方案

您正在使用spock进行单元测试,您应该能够轻松使用spock的MockingApi检查调用:

given:
    def mockXxService = Mock(XxService)
    service.xxService = mockXxService
when:
    service.yyy()
then:
    1 * mockXxService.xxx(_) //assert xxx() is called once

您可以从 spockframework文档中获得更多关于模拟的见解. /p>

您甚至可以在对相关服务进行以下模拟时对它进行存根和模拟:

def mockXxService = Mock(XxService) {
    1 * xxx(_)
}

In my unit test, I mock a service (which is a ref of the class under test).

Like:

given:
def mockXxService = mockFor(XxService)
mockXxService.demand.xxx(1) {->}
service.xxService = mockXxService
when:
service.yyy()
then:
// verify mockXxService's xxx method is invoked.

For my unit test, I want to verify that mockXxService.xxx() is called. But grails document's mockControl.verify() doesn't work for me. Not sure how to use it correctly.

It is very similar to mockito's verify method.

Anyone knows it?

解决方案

You are using spock for your unit test, you should be easily able to use spock's MockingApi check invocations:

given:
    def mockXxService = Mock(XxService)
    service.xxService = mockXxService
when:
    service.yyy()
then:
    1 * mockXxService.xxx(_) //assert xxx() is called once

You could get more insight about mocking from spockframework docs.

You can even stub and mock that while mocking the concerned service as:

def mockXxService = Mock(XxService) {
    1 * xxx(_)
}

这篇关于Grails单元测试验证模拟方法称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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