如何断言特定类的方法在另一个类的另一个方法之前被调用?

发布于 2024-11-07 02:30:37 字数 895 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-11-14 02:30:37

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

电影里的梦 2024-11-14 02:30:37

替代解决方案,验证在调用第二个方法时调用第一个方法:

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()
    {
        //Arrange
        anotherService.Setup(x=>x.AnotherMethod()).Callback(()=>{
            //Assert
            aService.Verify(x=>x.AMethod(), Times.Once());
        }).Verifyable();

        //Act
        testedClass.Run();
        //Assert
        anotherService.Verify();
    }
}

Alternate solution, verify the first method was called when the second one is being called:

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()
    {
        //Arrange
        anotherService.Setup(x=>x.AnotherMethod()).Callback(()=>{
            //Assert
            aService.Verify(x=>x.AMethod(), Times.Once());
        }).Verifyable();

        //Act
        testedClass.Run();
        //Assert
        anotherService.Verify();
    }
}
谁与争疯 2024-11-14 02:30:37

在每个模拟中记录时间戳并进行比较。

[Test]
public void ShouldCallAServiceMethodBeforeAnotherService()
{
    testedClass.Run();
    //Not sure about your mocking library but you should get the idea
    Assert(aService.AMethod.FirstExecutionTime 
        < anotherService.AnotherMethod.FirstExecutionTime, 
        "Second method executed before first");
}

Record a time stamp in each of your mocks, and compare them.

[Test]
public void ShouldCallAServiceMethodBeforeAnotherService()
{
    testedClass.Run();
    //Not sure about your mocking library but you should get the idea
    Assert(aService.AMethod.FirstExecutionTime 
        < anotherService.AnotherMethod.FirstExecutionTime, 
        "Second method executed before first");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文