gpt4 book ai didi

grails - Grails集成测试模拟未清除

转载 作者:行者123 更新时间:2023-12-02 15:53:27 24 4
gpt4 key购买 nike

我在两个无法解决的Spock集成测试之间遇到了一种奇怪的污染情况。我想我做错了什么,但我不明白。

两项集成测试正在测试同一 Controller 的不同情况。在第一个中,我 mock 服务,而在第二个中,我不 mock 。

这是两个测试的重要部分:

测试1:

// CodeControllerSpec.groovy
...
def controller = new CodeController()
def serviceMock = new MockFor(PollutingService)
serviceMock.demand.search(1) { a, b, c ->
return [id: 1]
}
controller.myService.pollutingService = serviceMock.proxyInstance()
controller.save()
...
then:
serviceMock.verify(controller.myService.pollutingService)

测试2:
// CodeEngineSpec.groovy
...
def controller = new CodeController()
controller.show()
...
then:
...

Controller 和服务如下
// CodeController
class CodeController extends RestfulController<Code> {
def myService

def show() {
...
myService.execute()
...
}
}

// MyService
class MyService {

def pollutingService

def execute() {
...
pollutingService.search(a, b, c)
...
}
}

// PollutingService
class PollutingService {

def search(a, b, c) {
...
...
}
}

如果我一个接一个地运行两个测试,则它们全部通过,但是,如果我一起运行它们,则第二个测试将失败。
No more calls to 'search' expected at this point. End of demands.
我确定使用了第一个服务中的模拟程序(我已经逐行调试了代码),但是我不知道为什么测试后没有清除模拟程序。
任何建议都非常欢迎。

我正在使用grails 2.3.8

最佳答案

首先,在集成测试中使用模拟具有不可预测的结果。

但是抛开这些,您的第一个测试中的controller.myService在哪里实例化?我本来希望调用controller = new CodeController()会绕过controller.myService的 Autowiring 。

关于grails - Grails集成测试模拟未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35598879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com