在服务类方法中测试命令对象,该方法将命令对象作为方法签名的一部分

时间:2014-05-29 20:26:16

标签: unit-testing grails spock

我有一个Grails服务类,我正在尝试为其编写Spock测试。该方法的签名如下:

def buildErrorJsonArray(AddressInfoCommand addressInfoCmd, PaymentInfoCommand paymentInfoCmd, boolean trsRequest = false) 

当我在测试中使用无效数据填充AddressInfoCommand和PaymentInfoCommand命令对象并对其调用validate时,它没有返回任何错误,我不知道为什么。我的猜测是我在测试中模拟命令对象的方式(通过mockForConstraintsTests)。以下是填充PaymentInfoCommand的测试部分:

setup:
    service.messageSource = [getMessage: { errors, locale -> return "message" }]
    mockForConstraintsTests(AddressInfoCommand)
    mockForConstraintsTests(PaymentInfoCommand)

    PaymentInfoCommand paymentInfoCommand = new PaymentInfoCommand()
    def payment = new Payment()
    payment.paymentType = ""
    payment.cardAccountNumber = ""
    payment.cardExpirationDate = ""
    payment.cardSecurityCode = ""
    paymentInfoCommand.setPaymentInfoCommand(payment)
    paymentInfoCommand.validate() 

这是PaymentInfoCommand的一部分:

class PaymentInfoCommand {
    String cardType = ""
    String cardAccountNumber = ""
    String cardExpirationDateYear = ""
    String cardExpirationDateMonth = ""
    String cardSecurityCode = ""
    def messageSource

    static constraints = {
        cardAccountNumber(nullable: false, blank: false, maxSize: 19, creditCard: true)

        cardSecurityCode(nullable: false, blank: false, minSize: 3, maxSize: 4, validator: {val, obj ->
            if (!StringUtils.isNumeric(obj.cardSecurityCode)) {
                return "paymentInfoCommand.cardSecurityCode.notANumber.error"
            }
        })
    } }

谁能看到我做错了什么?

0 个答案:

没有答案