gpt4 book ai didi

scala - Scala 对象(非类)的单元测试

转载 作者:行者123 更新时间:2023-12-02 12:51:19 25 4
gpt4 key购买 nike

我在 Scala 中有一个单例类,它被声明为“对象”。我需要编写单元测试来“模拟”该对象中的方法。我该怎么做?我尝试使用“PowerMockRunner”等,但没有成功。

例如:

object MyClass extends Serializable {
def apply(): Unit = { ..... }
def more methods....
}

现在我需要编写一个模拟“apply”方法的单元测试。我该怎么做?

最佳答案

简短的回答 - 你不应该对单例进行单元测试。

多头在这里 - Unit testing with singletons .

您可以将功能提取到特征并对特征进行单元测试。

不知道为什么你需要extends Serialized,但留下它只是为了展示我的意思

  object MyClass extends Serializable with TheFunctionality {
// whatever you need here
}

trait TheFunctionality {
def apply(): Unit = ???
}

关于scala - Scala 对象(非类)的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com