我在grails中有一组基本的单元测试,并且得到了一些奇怪的,不一致的行为。因此,基本上,单元测试用于与我的用户域进行交互的服务。
问题出在高水平-如果我运行完整的测试规范-每次4个测试都会失败。如果我单独运行它们-它们会通过。这里很明显的事情是,测试之间保持着状态-但是鉴于这是一个单元测试-不应存在(并且我已经通过记录验证了这一点)。
这里发挥的作用是:
因此,在查看规格时,我在设置中具有以下内容:
User user
def setup() {
println "================ Setting Up User (${User.count()}) ================"
// Set the roles
new Role(authority: "ROLE_ADMIN").save()
new Role(authority: "ROLE_OPERATOR").save()
def role = new Role(authority: "ROLE_USER").save()
def userAddress = new Address()
userAddress.with {
name = "Name"
address1 = "Address 1"
address2 = "Address 2"
townCity = "Town"
postcode = "BT19"
county = "Down"
}
// Create an admin user
user = new User()
user.with {
christianName = "Phil"
surname = "Preston"
email = "p@p.com"
username = "admin"
password = "password123"
phone = ""
skype = ""
address = userAddress
}
println(user.properties)
// Save the test user
if (user.validate()) {
println "Saving"
user.save(flush: true, failOnErrors: true)
UserRole.create(user, role)
}
else {
user.errors.allErrors.eachWithIndex { i, x ->
println "${i} - ${x}"
}
}
assert user
}
两个简单的测试如下:
void "Test updateUser - changing a user password"() {
given: "An update to the user"
UserCommand cmd = new UserCommand()
cmd.with {
id = user.id
christianName = user.christianName
surname = user.surname
username = user.username
email = user.email
skype = user.skype
phone = user.phone
password = "newPassword"
passwordCheck = "newPassword"
isAdmin = user.isAdmin()
isOperator = user.isOperator()
}
when: "attempt to update"
User updated = service.updateUser(cmd)
then: "the password should be update - but nothing else"
updated.password == cmd.password
}
void "Test updateUser - changing a user detail"() {
given: "An update to the user"
UserCommand cmd = new UserCommand()
cmd.with {
id = user.id
christianName = user.christianName
surname = user.surname
username = user.username
email = "update@update.com"
skype = user.skype
phone = user.phone
password = user.password
isAdmin = user.isAdmin()
isOperator = user.isOperator()
}
when: "attempt to update"
User updated = service.updateUser(cmd)
then: "the email should be update - but nothing else"
updated.email == cmd.email
}
(还有其他人,但这仅是为了演示问题)
因此,奇怪之处如下:
setup()
中的代码打印用户对象的数量(0)assert
和日志记录)单元测试失败,因为当我要验证的用户域对象的验证失败时,我抛出异常。因此,以下内容:
def updateUser(UserCommand userCommand) {
assert userCommand
// Get the current
User foundUser = User.findById(userCommand.id)
def wasAdmin = foundUser.admin
def wasOperator = foundUser.operator
// Bind Data
bindData(foundUser, userCommand, ['class', 'operator', 'admin', 'address'])
foundUser?.address?.with {
name = userCommand.name
address1 = userCommand.address1
address2 = userCommand.address2
townCity = userCommand.townCity
county = userCommand.county
postcode = userCommand.postcode
}
// THIS VALIDATION FAILS ON SECOND TEST
if (foundUser.validate()) {
foundUser.save()
// ... removed
return foundUser.refresh()
}
else {
Helper.copyErrorToCmd(foundUser, userCommand)
log.error("Errors: ${foundUser.errors.allErrors}")
throw new UserServiceException(cmd: userCommand, message: "There were errors updating user")
}
基本上(缩短)了用户域对象上的错误:
因此,您可以看到我在这些字段中使用了空字符串-并转换为null(如Grails所做的那样),这就是原因。但是,问题在于:
nullable: true
。因此,User域对象中的约束如下:
static constraints = {
christianName blank: false
surname blank: false
username blank: false, unique: true, size: 5..20
password blank: false, password: true, minSize: 8
email email: true, blank: false
phone nullable: true
skype nullable: true
address nullable: true
}
我正在使用具有以下约束的Command对象:
static constraints = {
importFrom User
importFrom Address
password blank: false, password: true, minSize: 8
passwordCheck(blank: false, password: true, validator: { pwd, uco -> return pwd == uco.password })
}
注意,我什至尝试在UserCommand约束闭包中添加Skype和phone字段。
如果我在
setup
的字段中添加文本,此问题就消失了,但这在实际的应用程序中是不可能的,因为这些字段可以留为空白。任何有关这种不一致情况如何发生的帮助将不胜感激。
RECREATION
我添加了一个最小的GitHub项目,它重新创建了这个问题:Github Project Recreating Issue。要查看问题:
./grailsw test-app
将运行两次测试,第一次通过第二次失败。要单独运行失败的测试,请运行:
./gradlew test --tests "org.arkdev.bwmc.accountmission.UserServiceSpec.*Test updateUser - changing a user detail"
您可以看到测试通过。
问题似乎是
skype
和phone
字段在第一个测试中为nullable:true,在第二个测试中为nullable:false(我进入user.validate()
并进入GrailsDomainClassValidator.java
,在validate(Object,Errors,boolean)
调用中可以看到constrainedProperties
映射(这基本上是字段->约束。)这表明在首次调用setup时,skype是NullableConstraint.nullable == true
,而对于第二项测试,则表明在setup调用中是NullableConstraint.nullable == false
。这是没有道理的。
最佳答案
第一次通话后
bindData(foundUser, userCommand, ['class', 'operator', 'admin', 'address'])
在
UserService.updateUser(UserCommand)
中(通过功能方法Test updateUser - changing a user password
调用),静态成员User.constraints
变为null
。去搞清楚。我不知道Grails是如何工作的,所以也许其他人也可以理解。但是在我看来,这不应该发生。也许您在那个电话中犯了一个错误。
关于unit-testing - Grails单元测试无效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43227084/