使用“在新的WithApplication中"时如何在specs2中进行设置/拆卸 [英] How to do setup/teardown in specs2 when using "in new WithApplication"

查看:71
本文介绍了使用“在新的WithApplication中"时如何在specs2中进行设置/拆卸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Specs2与使用Scala 2.10.2(运行Java 1.7.0_51)构建的播放2.2.1配合使用.我一直在阅读有关如何使用Specs2进行设置/拆卸的信息.我已经看到了使用"After"特征的示例,如下所示:

I am using Specs2 with play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_51). I have been reading about how to do setup/teardown with Specs2. I have seen examples using the "After" trait as follows:

class Specs2Play extends org.specs2.mutable.Specification {
  "this is the first example" in new SetupAndTeardownPasswordAccount {
    println("testing")
  }
}

trait SetupAndTeardownPasswordAccount extends org.specs2.mutable.After {
  println("setup")

  def after  = println("teardown ")
}

这很好,除了我所有的测试都使用在新的WithApplication中".看来我需要的是同时具有"WithApplication"和"After"的对象.下面不编译,但实际上是我想要的:

This works fine, except that all of my tests are using "in new WithApplication". It seems what I need is to have an object which is both a "WithApplication" and an "After". Below does not compile, but is essentially what I want:

trait SetupAndTeardownPasswordAccount extends org.specs2.mutable.After with WithApplication

所以,我的问题是,如何为已经在"WithApplication中使用"的测试添加设置/拆卸?我主要担心的是,我们所有的测试都使用了这种伪造的路由(因此他们需要With Application).

So, my question is, how do I add setup/teardown to my tests which are already using "in WithApplication"? My primary concern is that all of our tests make use of fake routing like this (so they need the With Application).

val aFakeRequest = FakeRequest(method, url).withHeaders(headers).withBody(jsonBody)
val Some(result) = play.api.test.Helpers.route(aFakeRequest)
result

推荐答案

这是WithApplication的代码:

abstract class WithApplication(val app: FakeApplication = FakeApplication()) extends Around with Scope {
  implicit def implicitApp = app
  override def around[T: AsResult](t: => T): Result = {
    Helpers.running(app)(AsResult.effectively(t))
  }
}

修改它以满足您的需求实际上很容易,而无需创建许多其他特征.这里缺少的部分是匿名函数t,您可以在测试中使用该函数(使用WithApplication)为其提供实现.如有必要,使WithApplication更加健壮,使其能够在测试之前和之后执行任意代码块即可.

It's actually quite easy to modify this to suit your needs without creating a bunch of other traits. The missing piece here is the anonymous function t, which you provide the implementation for in your tests (using WithApplication). It would be nice to make WithApplication a little more robust to be able to execute arbitrary blocks of code before and after the tests, if necessary.

一种方法可能是创建与WithApplication相似的类,该类接受两个均返回Unit的匿名函数setupteardown.我真正需要做的就是修改AsResult.effectively(t)内部的内容.为简单起见,我将从参数列表中删除app参数,并始终使用FakeApplication.您似乎并没有提供其他配置,可以随时将其添加回去.

One approach could be to create a similar class to WithApplication that accepts two anonymous functions setup and teardown that both return Unit. All I really need to do is modify what's happening inside AsResult.effectively(t). To keep this simple, I'm going to remove the app parameter from the parameter list, and use FakeApplication always. You don't seem to be providing a different configuration, and it can always be added back.

abstract class WithEnv(setup: => Unit, teardown: => Unit) extends Around with Scope {
  implicit def implicitApp = app
  override def around[T: AsResult](t: => T): Result = {
    Helpers.running(app)(AsResult.effectively{
         setup
         try {
             t
         } finally {
             teardown
         }
    })
  }
}

我首先调用setup,然后是t,然后是teardown,而不是简单地调用匿名函数t. try/finally块非常重要,因为spec2中失败的测试会引发异常,并且我们希望确保无论结果如何,都将执行teardown.

Instead of simply calling the anonymous function t, I first call setup, then t, then teardown. The try/finally block is important because failed tests in specs2 throw exceptions, and we want to be sure that teardown will be executed no matter what the outcome.

现在,您可以使用功能轻松设置测试环境.

Now you can easily setup test environments using functions.

import java.nio.files.{Files, Paths}

def createFolder: Unit = Files.createDirectories(Paths.get("temp/test"))

def deleteFolder: Unit = Files.delete("temp/test")

"check if a file exists" in new WithEnv(createFolder, deleteFolder) {
    Files.exists(Paths.get("temp/test")) must beTrue
}

(这可能无法编译,但是您可以理解.)

(This might not compile, but you get the idea.)

这篇关于使用“在新的WithApplication中"时如何在specs2中进行设置/拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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