开发者

How can I clear the database (domains) between easyb scenarios in Grails Integration Testing?

I am running an Integration Test for a Grails application. I am using the easyb plugin. The problem is that the database doesn't seem to get cleared out between Scenarios. My When I run standard Grails Integration Tests, the persistence context is cleared between each test. The easyb Stori开发者_开发知识库es are in the Integration folder, but the Grails Integration Test rules don't seem to apply here... So how do you make easyb clean up after itself?

P.S. I'm defining multiple scenarios in the same groovy file fwiw, but I don't think this is necessarily pertinent.


Just incase somebody like me is still dealing with this issue and looking for a way to rollback after each test scenario, below is a solution that works (thanks to Burt Beckwith's blog).

Wrap each easyb test scenario in a with transaction block and manually rollback at the end

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

The above test passes. Please post if you have a better solution to the problem


One possibility is to use transactions. I use this technique in java. You mark your test with transaction annotation. And after the test you rollback database changes.

Next possibility is to run SQL cleanup queries in after scenario section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜