作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用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-10284和GRAILS-11268deepValidate
在关联或关联字段上不起作用。我期望您的测试结果如下:
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/
在我的 grails 应用程序中运行在 tomcat 上并在应用程序服务器之间进行 session 复制,当我如下在 http session 中保存 map 时,我得到一个异常 GrailsPara
无法解析类org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap @第4行,第1列。 导入org.codehaus.groovy.g
我是一名优秀的程序员,十分优秀!