小编Joh*_*n J的帖子

模拟返回 fs2.Stream 的方法

为什么不能用 Mockito 模拟来模拟返回 fs2.Stream 的方法?

Mockito 抱怨我试图返回 FreeC 而不是 Stream。为什么会这样,我怎样才能让它工作?

以下代码:

import cats.effect.IO
import fs2.Stream
import org.mockito.Mockito.when
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar

trait MyTrait {
  def method: Stream[IO, Int]
}

class TestSpec extends FlatSpec with MockitoSugar {

  val m: MyTrait = mock[MyTrait]
  val emptyReturn: Stream[IO, Int] = Stream.emits(List.empty[Int])

  when(m.method).thenReturn(emptyReturn)

  ...
}
Run Code Online (Sandbox Code Playgroud)

给出这个错误:

TestSpec:
TestSpec *** ABORTED ***
  org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Stream cannot be returned by method()
method() should return FreeC
***
If you're unsure why you're getting above error read on.
Due to …
Run Code Online (Sandbox Code Playgroud)

scala mockito fs2

5
推荐指数
2
解决办法
536
查看次数

标签 统计

fs2 ×1

mockito ×1

scala ×1