我正在使用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](_, _) (_, _))
^
我不喜欢这段代码的是,模拟期望在测试中的几个地方使用,并且这个丑陋的签名到处重复,但具有不同的 In/Out 类型参数和期望。
所以我想我会写一堂课
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]))
}
}
您可以使用以下代码:
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。
不要使用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]]
}