0

我创建了一个类似于下面提到的工作流处理器特征:

import org.scalatestplus.play._
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
import org.scalatest.Matchers._
import org.scalamock.scalatest.MockFactory
import utils.OAuthUtils._
import utils.OAuthUtils
import utils.OAuthProcess
import play.api.Application
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import play.api.libs.json.JsResult
import play.api.libs.json.Json
import play.api.libs.json.JsSuccess
import play.api.libs.json.JsError
import play.api.libs.ws.WS



trait MyProcess[A] {
  type B
  type C
  type D
  def stage1(s: String): B
  def stage2(b: B)(implicit e: ExecutionContext, app: Application, a: A):       Future[C]
  def stage3(c: C)(implicit e: ExecutionContext, app: Application, a: A): Future[JsResult[D]]
  def process(s: String)(implicit e: ExecutionContext, app: Application, a: A): Future[JsResult[D]] = {
   val b = stage1(s)
   val f_d = for {
      c <- stage2(b)
      d <- stage3(c)
   } yield d
   f_d
 }
}

现在我想对这段代码进行单元测试。所以我创建了一个更好的小 scalatest scalamock 测试套件:

class TestController extends PlaySpec with Results with MockFactory {
  "MyController" must {
    " testing for method process" in {
  running(FakeApplication()) {

    implicit val context = scala.concurrent.ExecutionContext.Implicits.global
    implicit val s = "implicit String"
    implicit val currentApp = play.api.Play.current

  case class TestParms(s: String)
  abstract class TestProcessor extends MyProcess[TestParms] {
      type B = String
      type C = String
      type D = String
    }

    implicit val t = stub[TestProcessor]
    t.stage1(_: String).when(token).returns(testToken)

    (t.stage2(_: String)(_: ExecutionContext, _: Application, _: TestParms)).when(token, context, currentApp, tp).returns({
      Future.successful(stage2String)
    })
    (t.stage3(_: String)(_: ExecutionContext, _: Application, _: TestParms)).when(token, context, currentApp, tp).returns({
      Future.successful(Json.fromJson[String](Json.parse(stage3String)))
     })
   }
 }
}
}

我的期望是将此存根设置在不同的类上并测试该类。存根(t.stage2 和 t.stage3)编译良好,但以下语句无法编译。

 t.stage1(_: String).when(token).returns(testToken)

编译器报告以下问题:

overloaded method value when with alternatives:   (resultOfAfterWordApplication: org.scalatest.words.ResultOfAfterWordApplication)Unit <and>   (f: => Unit)Unit  cannot be applied to (String)  TestController.scala  /play-scala/test/controllers

有人可以帮忙吗?我发现为 Scala 类编写单元测试以及模拟它们非常困难。

来自 Build.sbt 的 Scalatest 版本:

 "org.scalatestplus" %% "play" % "1.2.0" % "test",
 "org.scalamock" %% "scalamock-scalatest-support" % "3.2" % "test",
4

1 回答 1

0

尝试将调用包装在括号中

(t.stage1(_: String)).when(token).returns(testToken)

我相信 scalac 认为您正在尝试调用( 因为那将是 ) 返回when的实例。Stringt.stage1(_)

于 2015-06-14T21:45:26.107 回答