【问题标题】:Mockito mocking Akka StreamsMockito 嘲笑 Akka 流
【发布时间】:2020-02-17 21:45:13
【问题描述】:

在进行单元测试时,模拟涉及 Source、Flow 和 Sink 的 Akka Streams 调用的最佳方法是什么?


例如takeWhile函数:

def takeWhile(p: Out => Boolean): Repr[Out]

其中Repr 是特征中的特征:

trait FlowOps[+Out, +Mat] {
  import akka.stream.impl.Stages._
  import GraphDSL.Implicits._

  type Repr[+O] <: FlowOps[O, Mat] {
    type Repr[+OO] = FlowOps.this.Repr[OO]
    type Closed = FlowOps.this.Closed
  }

如果被测对象调用类似:mySource.takeWhile( ... ).runWith( ... ) 我可能需要模拟它...

你如何计算出如何模拟出来,例如,mock[Source[Any, Any]].takeWhile(*) returns mock[?]

SourceReprFlowOpsOut 之间的交互我不清楚。

FlowOps trait warns 的源代码,它是内部的,不应从它派生...这会影响模拟它吗?

【问题讨论】:

  • 为什么需要模拟流?我想从纯值/夹具创建测试流更容易。

标签: scala akka akka-stream mockito-scala


【解决方案1】:

你不需要为 Akka Streams 组件的基本测试模拟任何东西;只需使用testkit。如需灵感,请查看the tests in the Akka repository。以下是来自FlowTakeWhileSpec 的示例:

"take while predicate is true" in assertAllStagesStopped {
  Source(1 to 4).takeWhile(_ < 3).runWith(TestSink.probe[Int]).request(3).expectNext(1, 2).expectComplete()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2016-06-29
    • 1970-01-01
    相关资源
    最近更新 更多