如何在Grails Integration Testing中清除easyb场景之间的数据库(域)?

时间:2010-08-18 23:05:54

标签: unit-testing grails integration-testing easyb

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

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

将每个easyb测试场景包含在一个带事务块的位置,并在末尾手动回滚

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
}
}

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