Unit testing 如何在Grails集成测试中清除easyb场景之间的数据库(域)?

Unit testing 如何在Grails集成测试中清除easyb场景之间的数据库(域)?,unit-testing,grails,integration-testing,easyb,Unit Testing,Grails,Integration Testing,Easyb,我正在为Grails应用程序运行集成测试。我正在使用插件。问题在于,在不同的场景之间,数据库似乎没有被清除。当我运行标准Grails集成测试时,每次测试之间都会清除持久性上下文。easyb故事在Integration文件夹中,但是Grails集成测试规则似乎不适用于这里。。。那么,如何使easyb在其自身之后进行清理 另外,我在同一个groovy文件fwiw中定义了多个场景,但我认为这并不一定相关。一种可能性是使用事务。我在java中使用这种技术。使用事务注释标记测试。测试之后,您将回滚数据库更

我正在为Grails应用程序运行集成测试。我正在使用插件。问题在于,在不同的场景之间,数据库似乎没有被清除。当我运行标准Grails集成测试时,每次测试之间都会清除持久性上下文。easyb故事在Integration文件夹中,但是Grails集成测试规则似乎不适用于这里。。。那么,如何使easyb在其自身之后进行清理


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

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


下一种可能是在after scenario部分中运行SQL清理查询。

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


下一种可能是在“场景后”部分运行SQL清理查询。

如果像我这样的人仍在处理这个问题,并在每个测试场景后寻找回滚的方法,下面是一个有效的解决方案(感谢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
}
}
上述测试通过。
如果你对这个问题有更好的解决方案,请发帖子

以防万一像我这样的人仍在处理这个问题,并在每个测试场景后寻找回滚的方法,下面是一个有效的解决方案(感谢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
}
}
上述测试通过。 如果您有更好的解决方案,请发布