代码之家  ›  专栏  ›  技术社区  ›  vicatcu

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

  •  2
  • vicatcu  · 技术社区  · 15 年前

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

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

    2 回复  |  直到 15 年前
        1
  •  0
  •   scout    14 年前

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

    将每个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
    }
    }
    

    以上测试通过。

        2
  •  0
  •   amra    15 年前

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

    部分。

    推荐文章