我试图了解如何使用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); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。