2

我正在为 Grails 应用程序运行集成测试。我正在使用easyb插件。问题是数据库似乎没有在场景之间被清除。我在运行标准 Grails 集成测试时,会在每个测试之间清除持久性上下文。easyb Stories 位于 Integration 文件夹中,但 Grails 集成测试规则似乎不适用于此处……那么如何让 easyb 自行清理?

PS我在同一个groovy文件fwiw中定义了多个场景,但我认为这不一定是相关的。

4

2 回答 2

0

一种可能性是使用事务。我在java中使用这种技术。您使用事务注释标记您的测试。并在测试后回滚数据库更改。

下一个可能性是在场景部分之后运行 SQL 清理查询。

于 2010-08-19T19:22:11.940 回答
0

万一像我这样的人仍在处理这个问题并寻找在每个测试场景后回滚的方法,下面是一个可行的解决方案(感谢 Burt Beckwith 的博客)。

将每个 easyb 测试场景包装在一个 with 事务块中,并在最后手动回滚

scenario "add person should be successful", {
Person.withTransaction { status -> 
    given "no people in database", {
    }
    when "I add a person", {
        Person.build()
    }
    then "the number of people in database is one", {
        Person.list().size().shouldEqual 1
    }
    status.setRollbackOnly()
}
}

scenario "database rollback should be successful", {
given "the previous test created a person", {
}
when "queried for people", {
    people = Person.list().size()
}
then "the number of people should be zero", {
    people.shouldEqual 0
}
}

上述测试通过。如果您对问题有更好的解决方案,请发布

于 2011-11-14T22:45:35.083 回答