这是我所拥有的一个例子:
public class ClassToBeTestedTest { private Mock<IAService> aService; private Mock<IAnotherService> anotherService; private ClassToBeTested testedClass; [SetUp] public void setup() { aService = new Mock<IAService>(); anotherService = new Mock<IAnotherService>(); testedClass = new ClassToBeTested(aService.Object, anotherService.Object); } [Test] public void ShouldCallAServiceMethodBeforeAnotherService() { testedClass.Run(); aService.Verify(x=>x.AMethod(), Times.Once()); anotherService.Verify(x=>x.AnotherMethod(), Times.Once()); } }在这个样本中我只是检查它们是否被调用,没有其他序列...
我考虑在那些在测试类中添加某种序列控制的方法中设置回调...
编辑:我正在使用moq lib: http : //code.google.com/p/moq/
Here goes an example of what I have:
public class ClassToBeTestedTest { private Mock<IAService> aService; private Mock<IAnotherService> anotherService; private ClassToBeTested testedClass; [SetUp] public void setup() { aService = new Mock<IAService>(); anotherService = new Mock<IAnotherService>(); testedClass = new ClassToBeTested(aService.Object, anotherService.Object); } [Test] public void ShouldCallAServiceMethodBeforeAnotherService() { testedClass.Run(); aService.Verify(x=>x.AMethod(), Times.Once()); anotherService.Verify(x=>x.AnotherMethod(), Times.Once()); } }In this sample I just check if they were called, no mather the sequence...
Im considering to setup a callback in those methods that add some kind of sequence control in the test class...
edit: I'm using the moq lib: http://code.google.com/p/moq/
最满意答案
Rhino Mocks支持模拟中的订单,请参阅http://www.ayende.com/Wiki/Rhino+Mocks+Ordered+and+Unordered.ashx
或者Moq序列,或许, http: //dpwhelan.com/blog/software-development/moq-sequences/
请参阅此处以获取类似的问题, 如何使用Moq测试方法调用顺序
Rhino Mocks supports orders in the mocking, see http://www.ayende.com/Wiki/Rhino+Mocks+Ordered+and+Unordered.ashx
Or Moq Sequences perhaps, http://dpwhelan.com/blog/software-development/moq-sequences/
See here for a similar question on this, How to test method call order with Moq
更多推荐
发布评论