此时没有预期的呼叫。仍有 1 次调用预期错误

No call to expected at this point. Still 1 call(s) to expected error

在集成测试中,我使用 groovy 模拟功能来模拟如下所示的服务方法:

def mock = new groovy.mock.interceptor.MockFor(PaymentService)

mock.demand.processPayment(){ a, b, c-> ['status': true, 'approved': true, 'tresponse':  new TransactionResponse(amount: total.toBigDecimal(), transactionId: "4234234555", saleId: Sale.last().id, responseCode: "1", responseReasonCode: "1", responseReasonText: "approved", authorizationCode: "asdasd", paymentMethod: "CC", transactionType: "auth_capture", cardCodeResponse: "P").save(flush: true)]}


mock.use{
    controller.paymentService = new PaymentService()

    populateReceiptParams(total)

    controller.receipt()

}

支付控制器方法 receipt() 使用与 authorize.net 通信的支付服务方法 processPayment。所以我已经模拟了这个方法,如上所示。

当测试为运行时,我得到的错误如下

junit.framework.AssertionFailedError: No call to 'getMergedSale' expected at this point. Still 1 call(s) to 'processPayment' expected.
    at PaymentController.cart(PaymentController.groovy:296)
    at PaymentController.receipt(PaymentController.groovy:1096)

所以问题是在 receipt 方法中有另一个对 paymentservice 的调用

paymentService.getMergedSale([sessionAuth, userAuth])

那么这是否意味着必须在 getMergedSale 之前先调用模拟方法 processPayment?我感谢有关此错误原因的任何指南。谢谢!

So does this mean that the mocked method which is processPayment must be called first before getMergedSale?

不一定,没有。这确实意味着您需要提供 getMergedSale.

的模拟实现