使用Mockito嘲笑vararg元组



我想模拟此方法

def zadd[V: ByteStringSerializer](key: String, scoreMembers: (Double, V)*): Future[Long] 

尝试了此

mock.zadd(anyString(), Seq((anyDouble(), any String()), (anyDouble(), anyString())): _*) 

不起作用,因为Mockito说了3个比赛者,但得到了5个。

所以我尝试使用如何正确匹配Mockito中的Vararg

但是,我什至无法使用该示例中列出的代码 ArgumentMatchers.<String>any()在我的IDE中被标记为错误,说未识别类型字符串

Mockito-Scala在盒子外支持Varargs,例如

import cats.implicits._
import org.mockito.ArgumentMatchersSugar
import org.mockito.cats.IdiomaticMockitoCats
import org.mockito.scalatest.IdiomaticMockito
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
trait Foo {
  def zadd(key: String, scoreMembers: (Double, String)*): Future[Long]
}
class HelloSpec extends FlatSpec
    with Matchers
    with IdiomaticMockito
    with IdiomaticMockitoCats
    with ScalaFutures
    with ArgumentMatchersSugar {
  "Foo" should "mock varargs" in {
    val foo = mock[Foo]
    foo.zadd("", *) returnsF 42L
    foo.zadd("bar", (7.7, "qux")) returnsF 89L
    foo.zadd("", (1.1, "")).futureValue should be (42L)
    foo.zadd("bar", (7.7d, "qux")).futureValue should be (89L)
  }
}

其中

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.0.8",
  "org.mockito" %% "mockito-scala" % "1.5.13",
  "org.mockito" %% "mockito-scala-scalatest" % "1.5.13",
  "org.mockito" %% "mockito-scala-cats" % "1.5.13",
  "org.typelevel" %% "cats-core"   % "2.0.0-M4"
)

相关内容

  • 没有找到相关文章

最新更新