2013-10-03 51 views
2

我正在尝试使用Mockito为Web服务编写一个模拟。模拟应该使用play WS库来模拟POST请求。Scala Spec2 Mockito:复杂类型的参数匹配器

/** 
* 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 
} 

运行测试时我得到以下错误:

[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. 

它看起来对我的任何[...]使用复杂型(带嵌套类型参数)的表达不正确解决为匹配器。但是,我没有看到原始类型在哪里发挥作用。 指定参数Map[String,Seq[String]]的匹配器的正确方法是什么?

非常感谢!

回答

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

注意post居然有一对额外的隐含参数有:

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

...这可能需要显式地匹配,如this spec2-users thread

0

看来wsRequestHolder.post方法需要三个参数,因此Mockito希望您发送三个(例如任何[])匹配器,但您只为其中的一个提供了匹配器。