unit-testing - 使用 'traditional'记录/重播与Moq模型进行模拟

标签 unit-testing mocking moq

我是模拟的新手,正在决定使用模拟框架。 Moq home引号

Currently, it's the only mocking library that goes against the generalized and somewhat unintuitive (especially for novices) Record/Reply approach from all other frameworks.



谁能简单地解释一下“记录/重放”方法是什么,最小起订量有何不同?从决定框架的角度来看,每种方法的优缺点是什么?

谢谢。

最佳答案

RhinoMocks支持“记录/重放”方法。基本思想是将测试执行分为两个阶段,记录阶段和重播阶段。更具体一点

var repo = new MockRepository();
var dependency = repo.DynamicMock<IDependency>();
With.Mocks(repo).Expecting(delegate {
         Expect.Call(dependency.AMethod(1)).Return(result);                    
      }).Verify(delegate {
         var sut = new Sut(wrappee);
         sut.DoStuffThatCallsAMethod();
         Assert.IsTrue(sut.ResultState);
      });

因此,Expecting块是Record阶段,Verify块是Replay阶段。

此代码的Moq变体为
var dependency = new Mock<IDependency>();
dependency.Expect(dep => dep.AMethod(1)).Returns(result);          
var sut = new Sut(wrappee.Object);
sut.DoStuffThatCallsAMethod();
Assert.IsTrue(sut.ResultState);

正如您所看到的那样,阅读起来要好得多。我曾经使用RhinoMocks,但自从发现Moq之后,我才使用Moq。我发现它可以产生更具可读性的代码。因此,我的建议是选择Moq。

关于unit-testing - 使用 'traditional'记录/重播与Moq模型进行模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650494/

相关文章:

调试构建类型的 Android 单元测试失败

unit-testing - 将 asp.net 5 测试放在单独的程序集中

java - Guice-servlet 单元测试

python - mock.patch.object(... 和 mock.patch(

c# - 在设置和验证中具有相同参数的起订量

iOS 重复实现 --> 测试失败

ios - xctest - 如何测试新 View 是否在按下按钮时加载

unit-testing - 如何在 Go 中为结构编写模拟

c# - 模拟 DbContext.set<T>.Add() EF6

c# - 使用 Moq,我如何模拟带有 out 参数的 protected 方法?