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

Unit testing 在将命令对象作为方法签名的一部分的服务类方法中测试命令对象,unit-testing,grails,spock,Unit Testing,Grails,Spock,我有一个Grails服务类,我正试图为它编写Spock测试。该方法的签名如下: def buildErrorJsonArray(AddressInfoCommand addressInfoCmd, PaymentInfoCommand paymentInfoCmd, boolean trsRequest = false) 当我在测试中使用无效数据填充AddressInfoCommand和PaymentInfoCommand对象并对其调用validate时,它不会返回任何错误,我也不确定原因。

我有一个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() 
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"
            }
        })
    } }
下面是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() 
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"
            }
        })
    } }

有人知道我做错了什么吗?

您使用的是什么版本的Grails?可能是讨论了这个问题我正在使用版本2.2.3在同样的讨论之后,如果您删除@validatable注释并在config.groovy中添加grails.validatable.classes=[packagename.PaymentInfoCommand],它应该会起作用。我尝试了您的建议,删除了@validatable注释并添加了grails.validatable.classes=[packagename.PaymentInfoCommand]到Config.groovy,但仍然不走运。