gpt4 book ai didi

unit-testing - 当参数具有grailsparametermap属性时,如何测试验证错误?

转载 作者:行者123 更新时间:2023-12-02 14:52:22 24 4
gpt4 key购买 nike

我正在使用grails,我想断言某个字段是否有错误。这对于firstName,lastName很有效,但不适用于地址。基本上,以下参数会导致地址验证错误,因为对于区域“MARIOLAND”来说是 Not Acceptable 。断言应该通过,但是失败。也许我没有以正确的方式提取地址。感谢您的帮助!谢谢!

controller.params.putAll(

firstName : "asd",
lastName : "asd",
email : "asd@gmail.com",
password : "asd",
address : new GrailsParameterMap([country: 'usa', address1: "24 Willey Mayes Plaza", city: "San Francisco", area: "MARIOLAND", postalCode: "94107"], controller.params.getRequest())


)



def model = controller.create()
def user = model.userInstance
user.validate()


assertTrue user.errors.hasFieldErrors("address")
//the above assertion should pass since MARIOLAND on area of address causes validation error, but it fails

最佳答案

我认为这与以下两个尚待解决的问题有关。

GRAILS-10284GRAILS-11268
deepValidate在关联或关联字段上不起作用。我期望您的测试结果如下:

import grails.test.mixin.TestFor
import spock.lang.Specification
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

@TestFor(UserController)
@TestMixin(GrailsUnitTestMixin)
@Mock([User, Address])
class UserControllerSpec extends Specification {

void setup(){
mockForConstraintsTests User
}

void "test something"() {
given:
controller.params.putAll(
firstName : "asd",
lastName : "asd",
email : "asd@gmail.com",
password : "asd",
address : new GrailsParameterMap([country: 'usa',
address1: "24 Willey Mayes Plaza",
city: "San Francisco",
area: "MARIOLAND",
postalCode: "94107"], controller.request)
)

// Also fails if we set address to params
/*controller.params.address = [
country: 'usa', address1: "24 Willey Mayes Plaza",
city: "San Francisco", area: "MARIOLAND", postalCode: "94107"
]*/

when:
def model = controller.index()
def user = model.userInstance

then:
!user.validate() // FAILS
user.errors.hasFieldErrors("address")
}

void "test address"() {
given:
controller.params.putAll(
new GrailsParameterMap([country: 'usa',
address1: "24 Willey Mayes Plaza", city: "San Francisco",
area: "MARIOLAND", postalCode: "94107"],
controller.params.request) )

when:
def model = controller.address()
def address = model.addressInstance
mockForConstraintsTests Address

then:
!address.validate() // PASSES
address.errors.hasFieldErrors("area")
}
}

class User {
String firstName, lastName, email, password
Address address

static constraints = {
// Tests will pass if address validated explicitly
//address validator: { it.validate() }
}
}

class Address {
String country, address1, city, area, postalCode

static constraints = {
area notEqual: "MARIOLAND"
}
}

class UserController {
def index() {
def user = new User(params)
[userInstance: user]
}

def address(){
def address = new Address(params)
[addressInstance: address]
}
}

关于unit-testing - 当参数具有grailsparametermap属性时,如何测试验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com