如何真正测试后端并避免使用MockWS?

时间:2015-11-28 06:29:08

标签: scala playframework

我对Playframework(以及一般的Scala)相当新。目前我正在使用MockWS编写测试python后端服务的specs2单元测试。是的,我发现很好地测试我的前端是否使用与后端生成的输出相同的输出。但是,如果我真的想测试后端怎么办?我怎样才能避免使用MockWS?

"SomeService" should {
object Props {
  object Urls {
    val BaseUrl = "http://localhost:9002/someservice/"
    val Test = s"${BaseUrl}test"
  }
  val test = Test(value=true)
}

class NormalContext extends Scope {
  val ws = MockWS {
    case (GET, Props.Urls.Test) => Action {
      Ok(Json.toJson(Props.test))
    }
  }

  val abtestService = new SomeService(ws, Props.Urls.BaseUrl, 100)
}

"test mock" should {
  "return True" when {
    "call without any argument" in new NormalContext {
      val result = someService.getTest()
      whenReady(result) { result =>
        result must equal (true)
      }
    }
  }
}
}

我尝试介绍这个新的上下文,但得到了例外:java.lang.RuntimeException: There is no started application

有什么建议吗?

  class LiveBackEndContext extends Scope {
  import play.api.Play.current
  val ws: WSClient = WS.client
  val abtestService = new AbTestService(ws, Props.Urls.BaseUrl, 100)
}
  "test without mock" should {
  "return True" when {
    "call without any argument" in new LiveBackEndContext {
      val result = abtestService.getTest()
      whenReady(result) { result =>
        result must equal (true)
      }
    }
  }
}

0 个答案:

没有答案