单元测试 – Moq是否取代标准的VS’Assert’测试?
发布时间:2020-12-14 04:50:08 所属栏目:百科 来源:网络整理
导读:我试图了解如何使用Moq,但是我对Moq应该用于什么感到困惑.我的理解是,模拟框架用于生成在正常情况下难以创建的对象.我看过Moq的例子似乎不仅创建了Moq对象,而且还提供了测试对象的方法 – 这似乎消除了我们用于大多数单元测试的常用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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |