比赛中的装置! Scala的2个 [英] Fixtures in Play! 2 for Scala

查看:82
本文介绍了比赛中的装置! Scala的2个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Play中进行一些集成测试! 2适用于Scala应用程序.为此,我需要在每次测试之前加载一些夹具,以使数据库处于已知状态.

I am trying to do some integration testing in a Play! 2 for Scala application. For this, I need to load some fixtures to have the DB in a known state before each test.

目前,我只是在调用一种方法,该方法执行一堆Squeryl语句以加载数据.但是,无论是使用Scala DSL还是使用JSON或YAML之类的声明性声明灯具,都更具可读性和易于维护性.

At the moment, I am just invoking a method that executes a bunch of Squeryl statements to load data. But declaring the fixtures declaratively, either with a Scala DSL or in a language like JSON or YAML is more readable and easy to mantain.

在我看到的Java应用程序的此示例中灯具是从YAML文件加载的,但是等效的Scala 应用程序会像我现在正在做的那样依靠手动加载.

In this example of a Java application I see that fixtures are loaded from a YAML file, but the equivalent Scala app resorts to manula loading, as I am doing right now.

我还发现了这个项目,它的文档记录不是很好,而且似乎比它复杂一些.我想-我什至不清楚实际在哪里声明了夹具数据.

I have also found this project which is not very well documented, and it seems a bit more complex than I'd like - it is not even clear to me where the fixture data is actually declared.

在Play中是否还有其他选项可以加载灯具!申请吗?

Are there any other options to load fixtures in a Play! application?

推荐答案

使用Evolutions.为SQL中的灯具编写安装和拆卸脚本,或使用mysqldump(或您的数据库的等效文件)将现有的测试数据库导出为sql.

Use Evolutions. Write a setup and teardown script for the fixtures in SQL, or use mysqldump (or equivalent for your DB) to export an existing test DB as sql.

http://www.playframework.org/documentation/1.2/evolutions

我发现进行测试的最轻松的方法是在内存数据库中进行所有设置,这意味着测试可以快速运行并使用JUnit从Java驱动测试.我使用H2DB,但需要注意一些陷阱.我通过艰辛的方式学习了这些知识,因此可以节省一些时间.

I find the most stress-free way to do testing is to set everything up in an in-memory database which means tests run fast and drive the tests from Java using JUnit. I use H2DB, but there are a few gotchas you need to watch out for. I learned these the hard way, so this should save you some time.

Play有一个不错的系统,可以使用running(FakeAplication()){..}来设置和拆卸应用程序以进行集成测试,并且可以将其配置为使用带有FakeApplication(additionalConfiguration = inMemoryDatabase( ))参见:

Play has a nice system for setting up and tearing down your application for integration testing, using running( FakeAplication() ) { .. }, and you can configure it to use an in memory database with FakeApplication(additionalConfiguration = inMemoryDatabase()) see:

http://www.playframework.org/documentation/2.0/ScalaTest

OutOfMemory错误::但是,在我的计算机上运行几次大型测试夹具会导致OutOfMemory错误.这似乎是因为inMemoryDatabase()函数的默认实现会创建一个新的随机命名的数据库,并且不会在两次测试运行之间清除旧的数据库.如果您正确编写了Evolution拆卸脚本,则不需要这样做,因为在每次测试之间,数据库将被清空并重新填充.因此,我们改掉了使用同一数据库的行为,并且内存问题消失了.

OutOfMemory errors: However, running a sizeable test fixture a few times on my machine caused OutOfMemory errors. This seems to be because the default implementation of the inMemoryDatabase() function creates a new randomly named database and doesn't clean up the old ones between test runs. This isn't necessary if you've written your evolution teardown scripts correctly, because the database will be emptied out and refilled between each test. So we overrode this behaviour to use the same database and the memory issues disappeared.

数据库方言::另一个问题是我们的生产数据库是MySQL,该数据库与H2DB有许多不兼容之处. H2DB具有许多数据库的兼容模式,这将减少您遇到的问题:

DB Dialect: Another issue is that our production database is MySQL which has a number of incompatibilities with H2DB. H2DB has compatibility modes for a number of dbs, which should reduce the number of problems you have:

http://www.h2database.com/html/features.html#compatibility

将所有内容放在一起会使每次测试之前添加起来有点笨拙,所以我将其提取到一个函数中:

Putting this all together makes it a little unwieldy to add before each test, so I extracted it into a function:

def memDB[T](code: =>T) = 
  running( FakeApplication( additionalConfiguration = Map( 
    "db.default.driver" -> "org.h2.Driver", 
    "db.default.url"    -> "jdbc:h2:mem:test;MODE=MySQL" 
  ) ) )(code) 

然后您可以像这样使用它(规格示例):

You can then use it like so (specs example):

"My app" should {
  "integrate nicely" in memDB {
    .....
  }
}

每项测试都会启动一个伪造的应用程序,运行您的灯具设置演变脚本,运行测试,然后将其全部拆掉.祝你好运!

Every test will start a fake application, run your fixture setup evolutions script, run the test, then tear it all down again. Good luck!

这篇关于比赛中的装置! Scala的2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆