【问题标题】:Integration Tests started to fail with grails upgrade随着 grails 升级,集成测试开始失败
【发布时间】:2011-08-26 13:26:14
【问题描述】:

在此升级后,我将一个 grails 应用程序从 1.2.2 升级到 1.3.7,一些集成测试开始引发以下错误,“validateAndSaveList”是我正在测试的服务使用的服务上的一种方法。这些测试在升级之前就通过了,如果我只使用 grails test-app -integration 运行集成测试阶段,它们也会通过

junit.framework.AssertionFailedError: 不再调用“validateAndSaveList” 预计在这一点上。结束 要求。

代码:

import com.e.domain.*
import com.e.exception.GORMServiceException
import com.e.controller.SecurityUserCommand



class AccountServiceTests extends GroovyTestCase
{
def accountService

void testRegisterWithMinimumInfo()
{
    def clinic = new Clinic(name:'clinicName')
    def securityUserCommand = new SecurityUserCommand(username:'username', password:"password", confirm:"password")
    def clinicUser = new ClinicUser(firstName:'fname', lastName:'lname', emailAddress:'abc@abc.com')
    clinicUser.clinic = clinic
    //clinicUser.securityUser = securityUser
    clinic.address = new Address()
    //  TODO - JsecUser no longer in use        
    def role = new ShiroRole(name:'TEST')
    //def subscription = ESubscription.findByName('Charter Member')
    def subscription = new ESubscription(
        name:'Charter Member',
        description:'Charter Member',
        periodType:'Monthly',
        numPeriods:12,
        amountPerPeriod:25.00,
        electronicSubmissionRate:0.00,
        accountingRate:0.01,
        numAllowedUsers:4,
        startDate: today -1,
        endDate: today+1
    )
    subscription.save(flush:true)
    if(subscription.hasErrors())
    println subscription.errors
    assertNotNull subscription
    clinicUser.empathicCustomerProfile.subscription = subscription
    def result = accountService.register(clinic, securityUserCommand, clinicUser, role)
    assert result.success
    assert result.clinic.id
    assert result.securityUser?.id
    assert result.clinicUser.id
}

堆栈跟踪

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)

【问题讨论】:

  • 请发布代码和堆栈跟踪。看起来你的模拟的期望失败了
  • 已更新代码和堆栈跟踪
  • 如果我将它作为 grails test-app AccountService 运行,测试也会通过,但当我作为 grails test-app 运行时不会通过
  • 您在哪里设置 accountService.register 的期望值?
  • 除了您在此处看到的内容和服务中的实际逻辑之外,什么都没有做。

标签: grails


【解决方案1】:

这个答案来自于在 cmets 中解决问题:

您得到的异常清楚地表明您在服务中的某个位置放置了一个模拟对象,并且该服务正在以未设置为处理的方式调用该模拟对象。

【讨论】:

  • 控制器的单元测试在测试中执行了以下操作: def dataBindServiceControl = mockFor(DataBindService) dataBindServiceControl.demand.safeBind{} dataBindServiceControl.demand.extractPhones{} dataBindServiceControl.demand.validateAndSaveList{ l-> return true} def dataBindService = dataBindServiceControl.createMock() controller.dataBindService = dataBindService
【解决方案2】:

从@hvgotcodes 看到的根本问题是服务有一个模拟对象,即使在给定的测试中没有模拟发生。

这发生在 grails 1.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

如果删除了这些测试,那么所有集成测试都会通过,因此无需重写测试即可解决,我在 tear down 方法中添加了以下内容。

    GroovySystem.metaClassRegistry.removeMetaClass(DataBindService)

添加此功能后,测试现在可以在 grails 1.3.7 中正常运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多