扩展特征的单元测试类 - 如何模拟和存根特征中的方法? [英] Unit testing class that extends a trait - how do I mock and stub methods in the trait?

查看:51
本文介绍了扩展特征的单元测试类 - 如何模拟和存根特征中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scalatest 对扩展 trait 的类进行单元测试(从某种意义上说,我的类正在使用 trait 作为 mixin).

I'm using scalatest to unit test a class that extends a trait (in the sense that my class is using the trait as a mixin).

特征包含作为辅助方法的方法(最终调用数据库中的一些查找),我想将其存根,以便我可以仅隔离我的类的功能.但是我还没有找到像 Mockito 或 ScalaMock 这样的框架来实现这一点.

The trait contains methods that are helper methods (which ultimately call a few lookups in a database) which I would like to stub out so that I can isolate just the functionality of my class. But I havent been able to find a framework like Mockito or ScalaMock that makes that possible.

问题:是否可以通过模拟框架实现,如果可以,如何实现?如果没有,我很想知道是否有原因.

Question: Can it be achieved with a mocking framework, and if so how? And if not, I'd be interested to know if there's a reason why.

例如

trait MyTrait {
  def usefulMethod(i: int) = {...}
}


class MyClass extends MyTrait {
  def numberCruncher = {
     val x = usefulMethod(1) + 1
  }
}

我需要根据 i 的值返回不同的存根答案.类似于 Mockito 的 when(myTrait.usefulMethod(1)).thenReturn(10)

I need to return a different stubbed answer depending on the value of i. Something akin to Mockito's when(myTrait.usefulMethod(1)).thenReturn(10)

我还需要验证是否在 trait 中调用了具有正确值的不同方法.

I also need to verify that a different method is called in the trait with the correct value.

本质上我在问这个问题再次但我注意到这是在 2011 年提出的问题,事情很可能已经发生了变化.可能会有新的框架和新的方法.这个 2001 年的问题也没有询问如何验证特征中的方法.

In essence I am asking this question again but I notice this was asked in 2011, and things may well have moved on. There may be new frameworks and new approaches. This 2001 question also doesnt ask about how to verify methods in the traits.

问题:以这种方式使用 traits-as-a-mixin 是否真的阻止了使用模拟框架从 mixin 模拟/存根方法进行单元测试的能力?即,mocking 和 stubbing 框架是否依赖于依赖注入的使用?

Question: Do the use of traits-as-a-mixin in this way actually prevent the ability to unit test using a mocking framework to mock/stub methods from the mixin? i.e. Do the mocking and stubbing frameworks depend on the use of dependency injection?

附注.到目前为止,我一直在使用 Mockito,但我并不反对使用 scalamock 或任何其他框架.我研究了 scalamock 是因为它宣传它可以测试特征,但从 scalamock 自己的测试看来,虽然它可以模拟特征,但如果该特征作为依赖项传递给类,而不是类,则只能存根模拟行为扩展该特性.请参阅 此处

PS. I've been using Mockito until now but I'm not averse to using scalamock or any other framework. I've looked into scalamock because it advertises that it can test traits but from scalamock's own tests it appears that while it can mock traits you can only stub mock the behaviour if that trait is passed as a dependency to a class, rather than a class extending that trait. See here

推荐答案

我通常对被测类进行子类化以替换我想模拟的方法:

I generally subclass the class under test to replace methods I want to mock out:

var usefulInput: Option[Int] = Nont
val testInstance = new MyClass {
  override def usefulMethod(i: Int) = {
    usefulInput = Some(i)
    42
  }
}

testInstance.numberCruncher should equal(43)
usefulInput should equal(Some(1))

我发现内联覆盖通常比任何模拟框架更易于实现和阅读

I find inline overriding generally simpler to implement and read than any mocking framework

这篇关于扩展特征的单元测试类 - 如何模拟和存根特征中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆