我是模拟的新手,正在决定使用模拟框架。 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/