模拟 Grails 单元测试的方法

发布于 2024-11-25 13:14:50 字数 754 浏览 0 评论 0原文

在我的一个单元测试中,我在执行模拟方法时遇到一些困难。我有以下测试代码:

void testExample() {
    def mockICFService = new MockFor(ICFService)
    ...

    //Mock the methods
    controller.metaClass.icfList = { def person ->
        println "icfList"
        return [new IC(conceptId:'12345')]
    }
    mockICFService.demand.getAllIC(1..1) { def id, def withHist, def appId ->
        println "mocking service"
        return new Person()
    }
    ...

    def model = controller.detail()
}

在我的控制器类的详细信息内部,我通过 ICFService 的 getAllIC() 创建一个 Person。这部分工作正常。然而,在该函数的后面,调用了 icfList(在控制器中定义)。通过 println,我确定该调用仍在进行,尽管它返回一个空数组。我相信这是因为该数组是根据 servletContext 中的数据填充的,但在单元测试中无法访问该数组(因此我尝试模拟它)。

有谁知道如何强制测试使用controller.icfList的模拟版本而不是调用控制器中的实际方法?

In one of my unit tests, I am having some difficulty getting a mocked method to executed. I have the following test code:

void testExample() {
    def mockICFService = new MockFor(ICFService)
    ...

    //Mock the methods
    controller.metaClass.icfList = { def person ->
        println "icfList"
        return [new IC(conceptId:'12345')]
    }
    mockICFService.demand.getAllIC(1..1) { def id, def withHist, def appId ->
        println "mocking service"
        return new Person()
    }
    ...

    def model = controller.detail()
}

Inside of detail in my controller class I create a Person via the ICFService's getAllIC(). This part works correctly. Later in the function, however, there is a call to icfList (which is defined in the controller). Through println's I have determined that the call is still being made, although it is returning an empty array. I believe that this is because the array is populated based on data in the servletContext, but in Unit Testing there is no access to that (hence my trying to mock it out).

Does anyone know how to force the test to use the mocked version of controller.icfList instead of calling the actual method in controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-12-02 13:14:50

当我尝试你的代码时,令我震惊的是模拟的服务,而正常工作的部分是模拟的 icfList() 方法。有趣的是,与你的观察相反。对于它的价值,这就是我所做的:

首先用 mockFor() 方法替换 new MockFor() 类实例化。然后您需要将模拟服务注入到控制器中。

def mockICFService = mockFor(ICFService)
controller.iCFService = mockICFService.createMock()

通过执行上述操作,只会调用 icfList() 和 getAllIC() 的模拟版本,因此您根本没有使用 servletContext。查看 Grails 测试文档了解更多信息。

When I try your code, what blows up for me is the mocked service, and the part that works properly is the mocked-out icfList() method. The opposite of your observation, interestingly. For what it's worth, here's what I did:

First replace new MockFor() class instantiation with the mockFor() method. Then you need to inject the mock service into the controller.

def mockICFService = mockFor(ICFService)
controller.iCFService = mockICFService.createMock()

By doing the above, only the mocked versions of icfList() and getAllIC() get called, so you are not using the servletContext at all. Check out the Grails testing documentation for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文