模拟返回 fs2 的方法.流



为什么不能模拟返回 fs2 的方法。用莫皮托模拟直播?

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)
...
}

给出此错误:

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 the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
at TestSpec.<init>(TestSpec.scala:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:435)
at sbt.TestRunner.runTest$1(TestFramework.scala:76)
at sbt.TestRunner.run(TestFramework.scala:85)
at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
...

我不确定嘲笑方面,但根本不需要使用嘲笑;

val m: MyTrait = new MyTrait {
def method: Stream[IO, Int] = Stream.emits(List.empty[Int]).covary[IO]
}

mockito 对模拟 (MyTrait) 和 fs2 的运行时检查似乎有些奇怪。流是 FreeC 的值类。

一种解决方案(除了不嘲笑之外)是强制 fs2。要在测试实现中的运行时装箱的流。 我通过将 MyTrait 转换为 MyTrait[F[_]] 和 def 方法:Stream[IO, Int] 转换为 def 方法:F[Int] 来管理这一点。

然后你嘲笑MyTrait[Stream[IO,?]]。

希望这有所帮助


此问题已在 mockito-scala v1.0.3 中修复

相关内容

  • 没有找到相关文章

最新更新