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

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

问题描述

我正在尝试在 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() 函数的默认实现会创建一个新的随机命名的数据库,并且不会在测试运行之间清理旧数据库.如果您正确编写了进化拆卸脚本,则这不是必需的,因为在每次测试之间数据库将被清空并重新填充.所以我们覆盖了这个行为以使用相同的数据库并且内存问题消失了.

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.

DB 方言:另一个问题是我们的生产数据库是 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!

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

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