Scalamock:无法模拟具有类型化参数和多个隐式变量的函数
这个问题是上文提到的已解决问题的延伸 我有以下特点要嘲弄:Scalamock:无法模拟具有类型化参数和多个隐式变量的函数,scala,unit-testing,mocking,scalatest,scalamock,Scala,Unit Testing,Mocking,Scalatest,Scalamock,这个问题是上文提到的已解决问题的延伸 我有以下特点要嘲弄: trait HttpClient{ def deserialize[T](response: HttpResponse) (implicit um: Unmarshaller[ResponseEntity, T], executionContext: ExecutionContext): Future[T] } 我试图模仿HttpClient,如下
trait HttpClient{
def deserialize[T](response: HttpResponse)
(implicit um: Unmarshaller[ResponseEntity, T],
executionContext: ExecutionContext): Future[T]
}
我试图模仿HttpClient,如下所示
val client = mock[HttpClient]
case class SomeTypeT(name:String, id:Int)
implicit val someTypeTFormat = jsonFormat2(SomeTypeT) // to be able to marshal and unmarshal from JSON
(httpClient.deserialize[SomeTypeT](_: HttpResponse))
.expects(where {
(response: HttpResponse) => {
response.entity == ent
}
})
.returns(Unmarshal(response.entity).to[SomeTypeT])
当我试图模拟反序列化函数时,问题就出现了。如上所述,反序列化方法包括一个类型化参数T和一个类型为HttpResponse的参数,以及在解组响应时使用的另外两个隐式参数
因此,问题是如何使用ScalaMock模拟反序列化函数,并在模拟时指定多个隐式参数。这行不通
// Both UnMarshaller & ExecutionContext are available here as implicits
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT](_: ExecutionContext))
问题是我不能使用u同时指定这两个隐式参数。我不知道如何做到这一点。请帮助如何模拟给定的函数
我正在使用以下库:
scala版本2.11.8
scalatest版本3.0.0
scalamock版本3.5.0
虽然第二次尝试由于使用了多个uu而无法编译,但第一次尝试会导致以下异常:
org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction1
java.lang.ClassCastException: org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction1
根据您在GitHub上链接的答案,您的代码应该是
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT], _:ExecutionContext))
.expects(where {
(response: HttpResponse, _: Unmarshaller[ResponseEntity, SomeTypeT], _:ExecutionContext) => {
response.entity == ent
}
})
.returns(Unmarshal(response.entity).to[SomeTypeT])
也就是说,在调用中明确地将隐式参数的占位符作为第二组参数放在调用中,在所有非隐式参数之后,将其作为附加参数放在where中。谢谢您的帮助。我没有在where语句中添加与您正确指出的相同的隐式参数。