我知道如何使用这些术语,但我想知道单元测试是否有伪造、模拟和存根的公认定义?如何为您的测试定义这些?描述一下你可能会用到它们的场景。

以下是我如何使用它们:

Fake:实现接口但包含固定数据且没有逻辑的类。只是根据实现返回“好”或“坏”数据。

Mock:实现接口的类,允许从特定方法动态设置返回值/抛出异常,并提供检查特定方法是否被调用/未调用的能力。

Stub:类似于模拟类,只是它不提供验证方法是否被调用的能力。

模拟和存根可以手工生成,也可以由模拟框架生成。假类是手工生成的。我使用模拟主要是为了验证我的类和依赖类之间的交互。一旦我验证了交互并在代码中测试替代路径,我就使用存根。我使用伪类主要是为了抽象出数据依赖关系,或者当mock /存根太乏味而每次都无法设置时。


当前回答

你在它上面断言的东西叫做模拟对象。

其他帮助测试运行的东西都是存根。

其他回答

Stub和fake是对象,因为它们可以根据输入参数改变响应。它们之间的主要区别是Fake比存根更接近真实世界的实现。存根基本上包含对预期请求的硬编码响应。让我们看一个例子:

public class MyUnitTest {

 @Test
 public void testConcatenate() {
  StubDependency stubDependency = new StubDependency();
  int result = stubDependency.toNumber("one", "two");
  assertEquals("onetwo", result);
 }
}

public class StubDependency() {
 public int toNumber(string param) {
  if (param == “one”) {
   return 1;
  }
  if (param == “two”) {
   return 2;
  }
 }
}

A mock is a step up from fakes and stubs. Mocks provide the same functionality as stubs but are more complex. They can have rules defined for them that dictate in what order methods on their API must be called. Most mocks can track how many times a method was called and can react based on that information. Mocks generally know the context of each call and can react differently in different situations. Because of this, mocks require some knowledge of the class they are mocking. a stub generally cannot track how many times a method was called or in what order a sequence of methods was called. A mock looks like:

public class MockADependency {

 private int ShouldCallTwice;
 private boolean ShouldCallAtEnd;
 private boolean ShouldCallFirst;

 public int StringToInteger(String s) {
  if (s == "abc") {
   return 1;
  }
  if (s == "xyz") {
   return 2;
  }
  return 0;
 }

 public void ShouldCallFirst() {
  if ((ShouldCallTwice > 0) || ShouldCallAtEnd)
   throw new AssertionException("ShouldCallFirst not first thod called");
  ShouldCallFirst = true;
 }

 public int ShouldCallTwice(string s) {
  if (!ShouldCallFirst)
   throw new AssertionException("ShouldCallTwice called before ShouldCallFirst");
  if (ShouldCallAtEnd)
   throw new AssertionException("ShouldCallTwice called after ShouldCallAtEnd");
  if (ShouldCallTwice >= 2)
   throw new AssertionException("ShouldCallTwice called more than twice");
  ShouldCallTwice++;
  return StringToInteger(s);
 }

 public void ShouldCallAtEnd() {
  if (!ShouldCallFirst)
   throw new AssertionException("ShouldCallAtEnd called before ShouldCallFirst");
  if (ShouldCallTwice != 2) throw new AssertionException("ShouldCallTwice not called twice");
  ShouldCallAtEnd = true;
 }

}

我倾向于使用两个术语-假和Mock。

只有在使用像Moq这样的Mock框架时才使用Mock,因为当它使用new Mock<ISomething>()创建时,将其称为Fake似乎是不正确的-虽然从技术上讲,您可以使用Mock框架创建stub或Fakes,但在这种情况下这样称呼它似乎有点愚蠢-它必须是Mock。

其他都是假的。如果一个Fake可以被总结为一个功能降低的实现,那么我认为一个Stub也可以是一个Fake(如果不是,谁在乎呢,每个人都知道我的意思,而且从来没有人说过“我认为你会发现那是一个Stub”)

我很惊讶这个问题已经存在了这么长时间,而且还没有人根据Roy Osherove的《单元测试的艺术》给出答案。

在“3.1介绍存根”中将存根定义为:

存根是现有依赖项的可控替代 (或合作者)在系统中。通过使用存根,您可以测试您的代码 直接处理依赖项。

并将存根和mock之间的区别定义为:

关于模拟和存根,要记住的主要事情是,模拟就像存根一样,但是您针对模拟对象进行断言,而不是针对存根进行断言。

Fake只是存根和mock的名称。例如,当您不关心存根和mock之间的区别时。

Osherove's区分存根和mock的方法意味着任何用于测试的伪类都可以是存根或mock。对于特定的测试,它完全取决于您在测试中如何编写检查。

当您的测试检查被测试类中的值,或者实际上除伪类之外的任何地方的值时,伪类被用作存根。它只是为被测试的类提供了可以使用的值,或者直接通过对它的调用返回值,或者间接地通过对它的调用导致副作用(在某些状态下)。 当您的测试检查假的值时,它被用作mock。

FakeX类被用作存根的测试示例:

const pleaseReturn5 = 5;
var fake = new FakeX(pleaseReturn5);
var cut = new ClassUnderTest(fake);

cut.SquareIt;

Assert.AreEqual(25, cut.SomeProperty);

假实例被用作存根,因为Assert根本不使用假实例。

测试类X被用作模拟的例子:

const pleaseReturn5 = 5;
var fake = new FakeX(pleaseReturn5);
var cut = new ClassUnderTest(fake);

cut.SquareIt;

Assert.AreEqual(25, fake.SomeProperty);

在本例中,Assert检查fake的值,使该fake成为mock。

当然,这些例子都是非常做作的,但我看到了这种区别的巨大价值。它让你意识到你是如何测试你的东西的,以及你的测试的依赖关系在哪里。

我同意Osherove的观点

从纯可维护性的角度来看,在我的测试中,使用mock比不使用它们更麻烦。这就是我的经验,但我一直在学习新的东西。

针对fake进行断言是您真正想要避免的事情,因为它会使您的测试高度依赖于一个根本不是被测试类的实现。这意味着类ActualClassUnderTest的测试可能开始中断,因为ClassUsedAsMock的实现改变了。这让我闻到一股恶臭。ActualClassUnderTest的测试最好只在ActualClassUnderTest被更改时中断。

我意识到针对虚假编写断言是一种常见的实践,特别是当您是TDD的嘲笑者类型时。我想我坚定地站在Martin Fowler的古典主义阵营(参见Martin Fowler的“mock不是Stubs”),并且像Osherove一样尽量避免交互测试(这只能通过断言反对虚假来完成)。

关于为什么你应该避免这里定义的mock的有趣阅读,谷歌代表“fowler mockist classical”。你会发现各种各样的观点。

在Gerard Meszaros所著的《xUnit Test Patterns》一书中,有一个很好的表格给出了关于差异的很好的见解

这是一个让测试富有表现力的问题。如果我想让测试描述两个对象之间的关系,我就在Mock上设置期望。我存根返回值,如果我设置一个支持对象,让我在测试中有趣的行为。