测试默认值

Testing for the Default Value

我刚开始为 Grails 应用程序编写测试,我已经看过 this and read this 并且想使用这些测试来检查是否已正确设置约束。

现在,我认为还应该测试默认值是否正确(如果有人篡改域对象,这是一种安全检查)。问题似乎是,如果我模拟一个对象,它就会存在,但不会设置默认值。有没有办法检查这个,或者我只是谨慎(拼写 'paranoid')?

应该变成这样:

class Event implements Taggable {

   ...
   Boolean publish
   ...

   static constraints = {
        ...
        publish nullable: true, defaultValue: true
        ...
    }
}

@TestMixin(GrailsUnitTestMixin)
@TestFor(Event)
class EventSpec extends Specification{

    Event ev = new Event(start: new Date(), subProgram: new Subprogram(),venue: new Venue())

    def setup() {
        mockForConstraintsTests(Event)
    }

    def cleanup() {
    }

    void "publish ist default true"() {  // cannot be tested ???

        expect:
        ev.publish
    }

是不是因为没有涉及到设置默认值的数据库(单元测试中没有可用的环境)?我只能在集成测试中这样做吗?

你是对的,你需要在集成测试中执行此操作,因为并非所有数据库组件都连接到单元测试中。