如何断言特定类的方法在另一个类的另一个方法之前被调用?
这是我所拥有的一个示例:
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());
}
}
在这个示例中,我只是检查它们是否被调用,而不管顺序...
我正在考虑在那些在测试类中添加某种序列控制的方法中设置回调...
编辑:我正在使用起订量库: 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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Rhino Mocks 支持模拟中的顺序,请参见 http://www .ayende.com/Wiki/Rhino+Mocks+Ordered+and+Unordered.ashx
或者最小起订量序列,http://dpwhelan.com/blog/software-development/moq-sequences/
请参阅此处有关此问题的类似问题,如何使用最小起订量测试方法调用顺序
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
替代解决方案,验证在调用第二个方法时调用第一个方法:
Alternate solution, verify the first method was called when the second one is being called:
在每个模拟中记录时间戳并进行比较。
Record a time stamp in each of your mocks, and compare them.