Scalamock:无法使用类型化参数和多个隐式变量模拟函数
Scalamock: Unable to mock function with typed parameter and multiple implicit variables
此问题是 https://github.com/paulbutcher/ScalaMock/issues/79
中提到的已解决问题的延伸
我有以下特征可以模拟:
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])
当我尝试模拟反序列化函数时出现问题。如上所述,deserialize
方法由一个类型化参数 T
和一个类型为 HttpResponse
的参数组成,以及在解组响应时使用的另外 2 个隐式参数。
因此,问题是如何使用 ScalaMock 模拟 deserialize
函数并在模拟时指定多个隐式参数。这不起作用
// Both UnMarshaller & ExecutionContext are available here as implicits
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT](_: ExecutionContext))
问题是我不能使用 _
来指定两个隐式参数。而且我不知道如何实现这一目标。请帮助如何模拟给定的函数
我正在使用以下库:
- scala 版本 2.11.8
- 最新版本 3.0.0
- scalamock 版本 3.5.0
由于使用了多个 _
,第二次尝试甚至无法编译,而第一次尝试导致以下异常:
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
中作为所有非隐式之后的附加参数。
此问题是 https://github.com/paulbutcher/ScalaMock/issues/79
中提到的已解决问题的延伸我有以下特征可以模拟:
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])
当我尝试模拟反序列化函数时出现问题。如上所述,deserialize
方法由一个类型化参数 T
和一个类型为 HttpResponse
的参数组成,以及在解组响应时使用的另外 2 个隐式参数。
因此,问题是如何使用 ScalaMock 模拟 deserialize
函数并在模拟时指定多个隐式参数。这不起作用
// Both UnMarshaller & ExecutionContext are available here as implicits
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT](_: ExecutionContext))
问题是我不能使用 _
来指定两个隐式参数。而且我不知道如何实现这一目标。请帮助如何模拟给定的函数
我正在使用以下库:
- scala 版本 2.11.8
- 最新版本 3.0.0
- scalamock 版本 3.5.0
由于使用了多个 _
,第二次尝试甚至无法编译,而第一次尝试导致以下异常:
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
中作为所有非隐式之后的附加参数。