Scala Spec2 Mockito:具有复杂类型的参数匹配器 [英] Scala Spec2 Mockito: Argument matchers with complex types

查看:125
本文介绍了Scala Spec2 Mockito:具有复杂类型的参数匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Mockito为Web服务编写一个模拟程序.该模拟程序应使用Play WS库模拟POST请求.

I'm trying to write a mock for a web service with Mockito. The mock should simulate a POST request using the play WS library.

/**
 * Mock for the  Web Service
 */
case class WSMock() extends Mockito {
  val wsRequestHolder: play.api.libs.ws.WS.WSRequestHolder = mock[play.api.libs.ws.WS.WSRequestHolder]

  val wsResponse: play.api.libs.ws.Response = mock[play.api.libs.ws.Response]
  wsResponse.status returns 200
  wsResponse.body returns "BODY RESP FROM WS"

  val futureResponse = scala.concurrent.Future { wsResponse }

  wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse
}

运行测试时,出现以下错误:

When running the test I get the following error:

[error]     InvalidUseOfMatchersException: 
[error] Invalid use of argument matchers!
[error] 3 matchers expected, 1 recorded:
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:24)
[error] 
[error] This exception may occur if matchers are combined with raw values:
[error]     //incorrect:
[error]     someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error]     //correct:
[error]     someMethod(anyObject(), eq("String by matcher"));
[error] 
[error] For more info see javadoc for Matchers class.

在我看来,使用复杂类型(带有嵌套类型参数)的any [...]表达式无法正确解析为匹配器.但是,我看不到原始类型在哪里起作用. 为参数Map[String,Seq[String]]指定这样的匹配器的正确方法是什么?

It looks to me as the any[...] expression using a complex type (with nested type parameters) does not correctly get resolved into a matcher. However, I don't see where the raw type comes into play. What is the proper way to specify such a matcher for a parameter Map[String,Seq[String]]?

非常感谢!

推荐答案

wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse

请注意,post实际上在那里还有几个额外的隐式参数:

Note that post actually has a couple extra implicit parameters there:

def post [T] (body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]):
    Promise[Response]

...,可能需要明确匹配,例如此spec2-用户线程.

...which probably need to be matched explicitly, as in this spec2-users thread.

这篇关于Scala Spec2 Mockito:具有复杂类型的参数匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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