我在两个斯波克集成测试之间有一个奇怪的污染情况,我无法解决。我想我做错了什么,但我不明白是什么。
这两个集成测试正在测试同一控制器的不同情况。在第一个中,我嘲笑服务,而在第二个中,我不嘲笑。
以下是两个测试的重要部分:
测试 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:
...
控制器和服务如下
// 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.
我确定使用了第一个服务中的模拟(我已经逐行调试了代码),但我不知道为什么测试后没有清理模拟。任何建议都是非常受欢迎的。
我正在使用圣杯 2.3.8
首先,在集成测试中使用模拟会产生不可预测的结果。
但撇开这一点不谈,controller.myService
在第一次测试中在哪里被实例化?我本以为打电话controller = new CodeController()
会绕过controller.myService
的自动布线。