加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

是否可以在Scala中模拟/覆盖依赖项/导入?

发布时间:2020-12-16 18:09:57 所属栏目:安全 来源:网络整理
导读:我有一些看起来像这样的代码: package org.samidarko.actorsimport org.samidarko.helpers.Libclass Monitoring extends Actor { override def receive: Receive = { case Tick = Lib.sendNotification() }} 有没有办法从ScalaTest模拟/存根Lib,就像proxyqu
我有一些看起来像这样的代码:

package org.samidarko.actors

import org.samidarko.helpers.Lib

class Monitoring extends Actor {

  override def receive: Receive = {
    case Tick =>
       Lib.sendNotification()
    }
}

有没有办法从ScalaTest模拟/存根Lib,就像proxyquire用于nodejs一样?

我读过我可以使用依赖注入,但我宁愿不这样做

我唯一的选择是将我的lib作为类参数传递吗?

class Monitoring(lib: Lib) extends Actor {

有什么建议让它更可测试吗?谢谢

编辑:

Xavier Guihot的答案是一个有趣的方法,但我选择更改代码以进行测试.

我将Lib作为参数传递,并且我正在使用mockito进行模拟,它使代码更容易测试和维护,而不是遮蔽范围.

解决方法

此答案仅使用scalatest,不会影响源代码:

基本解决方案

假设你有这个src类(你要测试的那个,你想要模拟它的依赖):

package com.my.code

import com.lib.LibHelper

class MyClass() {
  def myFunction(): String = LibHelper.help()
}

和这个库依赖项(你想在测试MyClass时模拟/覆盖):

package com.lib

object LibHelper {
  def help(): String = "hello world"
}

我们的想法是在您的测试文件夹中创建一个类,该类将覆盖/遮蔽库.该类将具有与您要模拟的名称相同的名称和相同的包.在src / test / scala / com / external / lib中,您可以创建包含以下代码的LibHelper.scala:

package com.lib

object LibHelper {
  def help(): String = "hello world - overriden"
}

这样你可以用通常的方式测试代码:

package com.my.code

import org.scalatest.FunSuite

class MyClassTest extends FunSuite {
  test("my_test") {
    assert(new MyClass().myFunction() === "hello world - overriden")
  }
}

改进的解决方案允许为每个测试设置模拟的行为:

以前的代码清晰简单,但LibHelper的模拟行为对于所有测试都是相同的.人们可能希望有一种LibHelper方法产生不同的输出.因此,我们可以考虑在LibHelper中设置一个可变变量,并在每次测试之前更新变量,以便设置LibHelper的所需行为. (这仅在LibHelper是对象时才有效)

阴影LibHelper(src / test / scala / com / external / lib中的那个)应替换为:

package com.lib

object LibHelper {

  var testName = "test_1"

  def help(): String =
    testName match {
      case "test_1" => "hello world - overriden - test 1"
      case "test_2" => "hello world - overriden - test 2"
    }
}

最神圣的课程应该成为:

package com.my.code

import com.lib.LibHelper

import org.scalatest.FunSuite

class MyClassTest extends FunSuite {
  test("test_1") {
    LibHelper.testName = "test_1"
    assert(new MyClass().myFunction() === "hello world - overriden - test 1")
  }
  test("test_2") {
    LibHelper.testName = "test_2"
    assert(new MyClass().myFunction() === "hello world - overriden - test 2")
  }
}

非常重要的精度,因为我们使用的是全局变量,所以必须强制scalatest按顺序运行测试(不是并行).相关的scalatest选项(包含在build.sbt中)是:

parallelExecution in Test := false

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读