随着grails升级,集成测试开始失败
我将grails应用程序从1.2.2升级到1.3.7在这次升级之后,一些集成测试开始抛出以下错误“validateAndSaveList”是我正在测试的服务所使用的服务上的一个方法。这些测试是在升级之前通过的,如果我只使用grails test app-integration运行集成测试阶段,它们也会通过 junit.framework.AssertionFailedError: 不再调用“validateAndSaveList” 在这一点上是意料之中的。结束 要求 代码: 堆栈跟踪随着grails升级,集成测试开始失败,grails,Grails,我将grails应用程序从1.2.2升级到1.3.7在这次升级之后,一些集成测试开始抛出以下错误“validateAndSaveList”是我正在测试的服务所使用的服务上的一个方法。这些测试是在升级之前通过的,如果我只使用grails test app-integration运行集成测试阶段,它们也会通过 junit.framework.AssertionFailedError: 不再调用“validateAndSaveList” 在这一点上是意料之中的。结束 要求 代码: 堆栈跟踪 junit
junit.framework.AssertionFailedError: No more calls to 'validateAndSaveList' expected at this point. End of demands.
at grails.test.MockClosureProxy.doBeforeCall(MockClosureProxy.java:66)
at grails.test.AbstractClosureProxy.call(AbstractClosureProxy.java:74)
at grails.test.GrailsMock$_createMock_closure1.doCall(GrailsMock.groovy:125)
at com.e.service.AccountService.register(AccountService.groovy:46)
at com.e.service.AccountService$register.call(Unknown Source)
at AccountServiceTests.testRegisterWithMinimumInfo(AccountServiceTests.groovy:53)
这个答案来自于在评论中解决问题:
您得到的异常清楚地表明您在服务中的某个地方放置了一个模拟对象,并且该服务正在以未设置为处理的方式调用该模拟对象 从@hvgotcodes中看到的根本问题是,服务有一个模拟对象,即使在给定的测试中没有发生模拟 这发生在Grails1.3.7中 我发现一个单元测试正在执行以下操作:
def dataBindServiceControl = mockFor(DataBindService)
dataBindServiceControl.demand.safeBind{}
dataBindServiceControl.demand.extractPhones{}
dataBindServiceControl.demand.validateAndSaveList{l-> return true}
def dataBindService = dataBindServiceControl.createMock()
controller.dataBindService = dataBindService
如果删除了这些测试,那么所有的集成测试都将通过,因此不需要重写我在分解方法中添加的测试
GroovySystem.metaClassRegistry.removeMetaClass(DataBindService)
通过这一添加,测试现在可以在Grails1.3.7中正常工作了请发布代码和堆栈跟踪。看起来你的模拟的期望值是失败的,用代码和堆栈跟踪更新。如果我以grails测试应用AccountService运行测试,测试也会通过,但当我以grails测试应用AccountService运行测试时,测试不会通过。你在哪里设置AccountService的期望值。注册?除了你在这里看到的内容和控制器的单元测试在测试中执行了以下操作:def-dataBindServiceControl=mockFor(dataBindServiceControl)dataBindServiceControl.demand.safeBind{}dataBindServiceControl.demand.extractPhones{}dataBindServiceControl.demand.validateAndSaveList{l->return true}def dataBindService=dataBindServiceControl.createMock()controller.dataBindService=dataBindService
GroovySystem.metaClassRegistry.removeMetaClass(DataBindService)