scala - Scalamock scalatest - Not able to stub -
i have created workflow processor trait similar 1 mentioned below:
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 = { c <- stage2(b) d <- stage3(c) } yield d f_d } }
now unit test code. created nicer little scalatest scalamock test suite:
class testcontroller extends playspec results mockfactory { "mycontroller" must { " testing 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))) }) } } } }
my expectation set stub on different class , test class. stubs (t.stage2 , t.stage3) compile fine following statement doesn't compile.
t.stage1(_: string).when(token).returns(testtoken)
compiler reports following issue:
overloaded method value when alternatives: (resultofafterwordapplication: org.scalatest.words.resultofafterwordapplication)unit <and> (f: => unit)unit cannot applied (string) testcontroller.scala /play-scala/test/controllers
could help? finding difficult write unit tests scala classes mocking them.
scalatest versions build.sbt:
"org.scalatestplus" %% "play" % "1.2.0" % "test", "org.scalamock" %% "scalamock-scalatest-support" % "3.2" % "test",
try wrapping invocation in parens
(t.stage1(_: string)).when(token).returns(testtoken)
i believe scalac thinks you're trying call when
on instance of string
(since what's returned t.stage1(_)
).
Comments
Post a Comment