我正在努力成为一名优秀的小程序员并为我的 Grails 2.2.3 应用程序设置单元测试。使用 GORM 注入.save()
方法的单元测试显然没有持久化到模拟测试数据库。例如,以下是一项测试的组成:
@TestFor(TermService)
@Mock(Term)
class TermServiceTests {
void testTermCount() {
def t = new Term(code: "201310").save(validate: false, flush: true, failOnError: true)
println "Printing Term: " + t.toString()
assert 1 == Term.count() // FAILS
assert service.isMainTerm(t) // FAILS
}
}
我做了一个println
最终打印Printing Term: null
,这意味着 Term 没有保存并返回一个 Term 实例。第一个断言为假,Term.count()
返回 0。
有谁知道这可能是为什么?我有一个模拟 Term 和 TermService (我相信通过 TestFor 注释),所以我不太确定为什么这不起作用。谢谢!
编辑:这是我的学期课程。
class Term {
Integer id
String code
String description
Date startDate
Date endDate
static mapping = {
// Legacy database mapping
}
static constraints = {
id blank: false
code maxSize: 6
description maxSize: 30
startDate()
endDate()
}
}