【问题标题】:How to mock nested classes in scala如何在scala中模拟嵌套类
【发布时间】:2021-11-30 23:22:22
【问题描述】:

我有一个可以简化如下的类

class my {
  val b = new subClass()
  def a () = "not mocked"

  class subClass {
    def ret () = "not mocked"
  }
}

我需要一些方法来模拟 my.b.ret()

【问题讨论】:

  • 你尝试了什么?什么不工作?

标签: scala mocking mockito scalatest


【解决方案1】:

这些方法会起作用吗?

class my {
  val b = new subClass()
  def a () = b.ret()

  class subClass {
    def ret () = "not mocked"
  }
}

class your extends my {
  override val b = new nested()
  class nested extends subClass {
    override def ret() = "TA-DA"
  }
}

运行以下

val mine = new my
mine.a()

val yours = new your
yours.a()

打印not mockedTA-DA,具体取决于您实例化的对象。

您可以使用此代码here on Scastie

【讨论】:

  • 我认为 OP 的意思是一个带有 mockito 的模拟,以便在之后轻松地在模拟上应用断言。
  • 嗯,对...我没有注意到标签。谢谢!希望这对其他正在寻找手动模拟方法的读者有所帮助。
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 2021-02-27
  • 1970-01-01
  • 2011-08-11
  • 2012-09-13
  • 1970-01-01
相关资源
最近更新 更多