2010-08-18 44 views
2

我正在爲Grails應用程序運行集成測試。我正在使用easyb插件。問題在於數據庫似乎沒有在場景之間清除。我運行標準的Grails集成測試時,持久化上下文在每個測試之間被清除。 easyb故事在Integration文件夾中,但Grails集成測試規則似乎不適用於此...那麼如何讓easyb自行清理呢?如何在Grails集成測試中清除easyb場景之間的數據庫(域)?

P.S.我在同一個groovy文件fwiw中定義了多個場景,但我認爲這不一定恰當。

回答

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

上述測試通過。 如果您有更好的解決方案,請發帖

0

一種可能性是使用交易。我在java中使用這種技術。您使用交易註釋標記您的測試。在測試之後,您將回滾數據庫更改。

下一種可能性是在場景部分之後的中運行SQL清理查詢。

相關問題