如何断言在另一个类的另一个方法之前调用特定类的方法?(How can I assert that a method of a specific class was called before anot

编程入门 行业动态 更新时间:2024-10-27 07:30:35
如何断言在另一个类的另一个方法之前调用特定类的方法?(How can I assert that a method of a specific class was called before another method of another class?)

这是我所拥有的一个例子:

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

更多推荐

本文发布于:2023-07-22 10:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   断言   assert   method   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!