小编use*_*977的帖子

为什么将空字符串转换为null传递给Grails 2.4.0中的域对象的构造函数?

我是Groovy和Grails的新手.由于空字符串被转换为null,因此要测试持久化的域对象的Spock测试失败.这是代码.域对象,

class Todo {

    String name
    Date createdDate
    String priority
    String status

    static constraints = {
        priority blank: true
    }

}
Run Code Online (Sandbox Code Playgroud)

Spock规范,

@TestFor(Todo)
class TodoSpec extends Specification {

    void "test persist"() {
        when:
        new Todo(name: 't1', createdDate: new Date(), priority: "1", status: 'ok').save()
        new Todo(name: 't2', createdDate: new Date(), priority: '', status: 'ok').save()

        then:
        Todo.list().size() == 2
    }

}
Run Code Online (Sandbox Code Playgroud)

结果grails test-app

Todo.list().size() == 2
     |      |      |
     |      1      false
     [collab.todo.Todo : 1]
        at collab.todo.TodoSpec.test persist(TodoSpec.groovy:18)
Run Code Online (Sandbox Code Playgroud)

我找到的空字符串'' …

grails groovy unit-testing data-conversion spock

5
推荐指数
1
解决办法
2220
查看次数

标签 统计

data-conversion ×1

grails ×1

groovy ×1

spock ×1

unit-testing ×1