单元测试 – Moq是否取代标准的VS’Assert’测试?

我试图了解如何使用Moq,但是我对Moq应该用于什么感到困惑.我的理解是,模拟框架用于生成在正常情况下难以创建的对象.我看过Moq的例子似乎不仅创建了Moq对象,而且还提供了测试对象的方法 – 这似乎消除了我们用于大多数单元测试的常用Assert等方法的需要.

有人可以确认我是否认为Moq取代Assert等是正确的,还是我完全忽略了Moq的观点?

解决方法

像Moq这样的模拟框架并没有完全取代测试框架的Assert.有时它确实如此,有时却没有.

让我们首先区分嘲笑和存根.存根纯粹用于隔离并将某种受控行为注入被测系统(SUT).

模拟是存根的超集,能够验证是否在模拟上调用了某些内容.在Moq中,调用Verify()会使stub成为关于该方法的模拟.调用VerifyAll()会使所有设置的方法都被模拟.

建议的方法是在测试中应该只有一个模拟.从这个意义上讲,它类似于Assert – 你不应该在测试中验证多个东西.

回到最初的问题.如果您正在执行状态测试,那么您将使用零个或多个存根和一个断言.如果您正在进行交互测试,那么您将使用零个或多个存根和一个模拟.下面是一个示例,其中可能适合使用mocks和asserts来测试相同的服务.

public interface IAccountRepository {
  decimal GetBalance(int accountId);
  void SetBalance(int accountId,decimal funds);
}

public class DepositTransaction {

  IAccountRepository m_repo;

  public DepositTransaction(IAccountRepository repo) {
    m_repo = repo;
  }

  public decimal DepositedFunds {get; private set;};

  void Deposit(int accountId,decimal funds) {
    decimal balance = m_repo.GetBalance(accountId);
    balance += funds;
    m_repo.SetBalance(balance);

    DepositedFunds += funds;
  }
}

public class DepositTest {
  [TestMethod]
  void DepositShouldSetBalance() {
    var accountMock = new Mock<IAccountRepository>();
    accountMock.Setup(a=>a.GetBalance(1)).Returns(100); //this is a stub with respect to GetBalance

    var transation = new DepositTransaction(accountMock.Object);
    transation.Deposit(1,20);

    accountMock.Verify(a=>a.SetBalance(1,120)); //this is a mock with respect to SetBalance
  }
  [TestMethod]
  void DepositShouldIncrementDepositedFunds() {
    var accountMock = new Mock<IAccountRepository>();
    accountMock.Setup(a=>a.GetBalance(1)).Returns(100); //this is a stub with respect to GetBalance

    var transation = new DepositTransaction(accountMock.Object);
    transation.Deposit(1,20);
    transation.Deposit(1,30);

    Assert.AreEqual(50,transaction.DepositedFunds);

  }
}

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...