最小起订量中的SetupSequence [英] SetupSequence in Moq

查看:138
本文介绍了最小起订量中的SetupSequence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望模拟第一次返回0,然后在每次调用该方法时都返回1.问题是,如果该方法被调用了4次,我应该这样写:

I want a mock returns a 0 the first time, then returns 1 anytime the method was called. The problem is that if the method is called 4 times, I should write that :

mock.SetupSequence(x => x.GetNumber())
    .Returns(0)
    .Returns(1)
    .Returns(1)
    .Returns(1);

否则该方法返回null.

otherwise the method returns null.

有什么办法可以写出在第一次调用该方法之后的下一次调用,该方法返回1? 谢谢

Is there any way to write that the next times the method was called after the first time, the method returns 1 ? Thank you

让SetupSequence拥有更多操作员"是件好事吗?如果您认为是,则可以投票: http://moq.uservoice.com/forums/11304 -general/suggestions/2973521-setupsequence-more-operators

Is it good to have more "operators" for SetupSequence ? If you think YES you can vote : http://moq.uservoice.com/forums/11304-general/suggestions/2973521-setupsequence-more-operators

推荐答案

这并不是特别花哨,但我认为它会起作用:

That's not particulary fancy, but I think it would work:

    var firstTime = true;

    mock.Setup(x => x.GetNumber())
        .Returns(()=>
                        {
                            if(!firstTime)
                                return 1;

                            firstTime = false;
                            return 0;
                        });

这篇关于最小起订量中的SetupSequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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