如何避免在scalamock中重复模拟函数签名?

时间:2018-03-15 23:59:40

标签: scala scalamock

我使用scalamock来模拟这个类:

class HttpService {
  def post[In, Out]
    (url: String, payload: In)
    (implicit encoder: Encoder[In], decoder: Decoder[Out])
    : Future[Out] = ...
  ...
}

...所以我的测试类有一个像这样使用的模拟:

val httpService = mock[HttpService]

(httpService.post[FormattedMessage, Unit](_ : String, _ : FormattedMessage) (_ : Encoder[FormattedMessage], _: Decoder[Unit]))
          .expects("http://example.com/whatever",*, *, *)
          .returning(Future.successful(()))

显然我必须编写整个模拟函数签名。如果我只将下划线放在签名中,没有相应的类型,我会收到类似这样的错误:

[error] missing parameter type for expanded function ((x$1: <error>, x$2, x$3, x$4) => httpService.post[FormattedMessage, Unit](x$1, x$2)(x$3, x$4)) 
[error]       (httpService.post[FormattedMessage, Unit](_, _) (_, _))
                                                        ^

我对这段代码不喜欢的是模拟期望在测试中的几个地方使用,这个丑陋的签名在整个地方重复,但具有不同的输入/输出类型参数和期望。

所以我以为我会写一个班级

class HttpServiceMock extends MockFactory {
  val instance = mock[HttpService]
  def post[In, Out] = instance.post[In, Out](_ : String, _ : In) (_ : Encoder[In], _: Decoder[Out])
}

...并像这样使用它:

val httpService = new HttpServiceMock()

...

httpService.post[FormattedMessage, Unit]
      .expects("http://example.com/whatever",*, *, *)
      .returning(Future.successful(()))

...编译得很好,但是当我运行测试时,我得到以下错误:

java.lang.NoSuchMethodException: com.myapp.test.tools.HttpServiceMock.mock$post$0()
  at java.lang.Class.getMethod(Class.java:1786)
  at com.myapp.controllers.SlackControllerSpec.$anonfun$new$3(SlackControllerSpec.scala:160)
  at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
  at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
  at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
  at org.scalatest.Transformer.apply(Transformer.scala:22)
  at org.scalatest.Transformer.apply(Transformer.scala:20)
  at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
  at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
  at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)

如何修复此错误?还有其他方法可以避免一遍又一遍地重写模拟函数签名吗?

更新: 最后,模拟看起来像这样:

trait HttpServiceMock extends MockFactory {
  object httpService {
    val instance = mock[HttpService]

    def post[In, Out] = toMockFunction4(instance.post[In, Out](_: String, _: In)(_: Encoder[In], _: Decoder[Out]))
  }
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

trait HttpMockSupport {
  this: MockFactory =>
  val httpService = mock[HttpService]

  def prettyPost[In, Out]: MockFunction4[String, In, Encoder[In], Decoder[Out], Future[Out]] = {
    toMockFunction4(httpService.post[In, Out](_: String, _: In)(_: Encoder[In], _: Decoder[Out]))
  }
}

class AClassThatNeedsHttpServiceMocking extends FreeSpec with Matchers with MockFactory with HttpMockSupport {

  "HttpService should post" in {

    val url = "http://localhost/1"
    val input = "input"
    implicit val encoder: Encoder[String] = new Encoder[String] {}
    implicit val decoder: Decoder[String] = new Decoder[String] {}

    prettyPost[String, String]
      .expects(url, input, encoder, decoder)
      .returns(Future.successful("result"))

    httpService.post(url, input)
  }
}

它将常见的模拟放在一个特征中,可以在需要模拟HttpService的所有地方进行扩展,只需调用非丑陋的方法:)

更新1:

更新它以接受预期的参数。

更新2:

将prettyPost方法更新为通用方法,以便我们可以设置任何类型的期望。

Scalamock期待一个MockFunctionX。因此,在您的情况下,您所要做的就是将丑陋的函数转换为漂亮的函数,然后将其转换为MockFunctionX。

答案 1 :(得分:0)

不要使用Scalamock,使HttpService成为特征,并直接实施特征来模拟你需要的任何东西。例如。 (您可以将其粘贴到Scala REPL中,但请记住在结尾处按 Enter Ctrl + D ):

:rese
:pa
import scala.concurrent.Future

trait Encoder[A]
trait Decoder[A]

// HttpService.scala

trait HttpService {
  def post[In: Encoder, Out: Decoder](
    url: String, payload: In): Future[Out]
}

object HttpService extends HttpService {
  override def post[In: Encoder, Out: Decoder](
    url: String,
    payload: In):
    Future[Out] = ???
}

// HttpServiceSpec.scala

class Mock[Out](result: Future[Out]) extends HttpService {
  override def post[In: Encoder, Out: Decoder](
    url: String,
    payload: In):
    Future[Out] =
    // This is fine because it's a mock.
    result.asInstanceOf[Future[Out]]
}