如何测试方法调用顺序与起订量 [英] How to test method call order with Moq

查看:151
本文介绍了如何测试方法调用顺序与起订量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有:

    [Test]
    public void DrawDrawsAllScreensInTheReverseOrderOfTheStack() {
        // Arrange.
        var screenMockOne = new Mock<IScreen>();
        var screenMockTwo = new Mock<IScreen>();
        var screens = new List<IScreen>();
        screens.Add(screenMockOne.Object);
        screens.Add(screenMockTwo.Object);
        var stackOfScreensMock = new Mock<IScreenStack>();
        stackOfScreensMock.Setup(s => s.ToArray()).Returns(screens.ToArray());
        var screenManager = new ScreenManager(stackOfScreensMock.Object);
        // Act.
        screenManager.Draw(new Mock<GameTime>().Object);
        // Assert.
        screenMockOne.Verify(smo => smo.Draw(It.IsAny<GameTime>()), Times.Once(),
            "Draw was not called on screen mock one");
        screenMockTwo.Verify(smo => smo.Draw(It.IsAny<GameTime>()), Times.Once(), 
            "Draw was not called on screen mock two");
    }

但在我画生产code我对象的顺序并不重要。我可以做一个第一,两个也无所谓。然而,它应该作为重要的抽奖顺序很重要。

But the order in which I draw my objects in the production code does not matter. I could do one first, or two it doesn't matter. However it should matter as the draw order is important.

你如何(使用MOQ)确保方法是按照一定的顺序叫什么?

How do you (using Moq) ensure methods are called in a certain order?

修改

我摆脱了测试。抽签方法已经从我的单元测试中删除。我只好手动测试它的作品。在那里进行了测试,所以它不是所有坏的,虽然被带到一个单独的测试类的顺序逆转。

I got rid of that test. The draw method has been removed from my unit tests. I'll just have to manually test it works. The reversing of the order though was taken into a seperate test class where it was tested so it's not all bad.

感谢对他们正在调查该功能的链接。我当然希望它被添加很快,非常方便。

Thanks for the link about the feature they are looking into. I sure hope it gets added soon, very handy.

推荐答案

看来,目前未实现。请参见问题24:MockSequence 这个线程讨论这个问题。

It appears that it's not currently implemented. See Issue 24: MockSequence. This thread discusses the issue.

您可能会考虑修改你的测试,虽然。我一般觉得,为了测试导致脆弱的测试,它的测试经常实施细节。

You might consider revising your tests, though. I generally feel that testing order leads to fragile tests, as it's often testing implementation details.

编辑:我不知道,这解决了OP的问题。卢塞罗的回答可能会更有帮助。

I'm not sure that this addresses the OP's question. Lucero's answer may be more helpful.

这篇关于如何测试方法调用顺序与起订量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆