简体   繁体   English

如何在“ Return”中设置模拟为Moq的out参数的值?

[英]How to set the value of an out parameter Mocked in Moq within the “Return”?

I've some trouble while configuring my Moq. 设置最低起订量时遇到麻烦。

I've a mockup with a method that has out parameter. 我有一个带有out参数的方法的模型。 This out parameter is really used, and I need to populate it with some meaningful value depending on the parameter I receive. 这个out参数是真正使用的,根据我收到的参数,我需要用一些有意义的值填充它。

Currently, I've been unable to do this, is it possible? 目前,我无法执行此操作,可以吗? How? 怎么样?

Here is my current status: 这是我目前的状态:

Mock<IMyMock> _mock = new Mock<IMyMock>();
bool someFlag= false;
_mock.Setup(m=>m.GetSomething(It.IsAny<DateTime>(), It.IsAny<DateTime>(), out someFlag))
     .Returns((DateTime start, DateTime end, bool someFlagInternal)=>{
        IEnumerable<SomeOtherClass> otherClasses = GenerateMockedData(start, end);
        //Assign something to someFlag, depending on start and end
        someFlag=true;//This has no effects
     }) ;
new SomeController(_mock.Object);

In my SomeController , on a specific method, my real-non-mocked code calls it: 在我的SomeController ,在特定方法上,我的真实非模拟代码将其调用:

public class SomeController{}
    private IMyMock _someObjectThatWeDontKnowIsAMock;
    public SomeController(IMyMock someObjectThatWeDontKnowIsAMock) {
        _someObjectThatWeDontKnowIsAMock= someObjectThatWeDontKnowIsAMock;
    }

    private void ComputeNext() {
        Tuple<DateTime, DateTime> times = ComputeNextTimes();
        bool hasOverlap;
        IEnumerable<SomeOtherClass> otherClassesInstances = _someObjectThatWeDontKnowIsAMock.GenerateMockedData(times.Data1, times.Data2, out hasOverlap);
        if(hasOverlap) {
            //...
        }
    }
}

Every time I call my mock, it seems to returns the first value of someFlag , even if it is a static field that I modify. 每当我调用我的模拟时,即使它是我修改的静态字段,它似乎都会返回someFlag的第一个值。

The problem you are having is because the instance of the out parameter in the setup is different to the instance actually being used when exercising the test. 您遇到的问题是因为安装程序中out参数的实例与执行测试时实际使用的实例不同。

Taken from Moq Quickstart documentation 取自Moq快速入门文档

callbacks for methods with ref / out parameters are possible but require some work (and Moq 4.8 or later) 可以对具有ref / out参数的方法进行回调,但需要做一些工作(以及Moq 4.8或更高版本)

Create a delegate to handle the mock invocation. 创建一个委托来处理模拟调用。

 delegate IEnumerable<SomeOtherClass> GetSomethingCallback(DateTime start, DateTime end, out bool someFlag);

In the setup use It.Ref<Bar>.IsAny for the out parameter and use the delegate in the Returns expression. 在安装程序中,将It.Ref<Bar>.IsAny用于out参数,并在Returns表达式中使用委托。

mock
    .Setup(_ => _.GetSomething(It.IsAny<DateTime>(), It.IsAny<DateTime>(), out It.Ref<bool>.IsAny))
    .Returns(new GetSomethingCallback((DateTime start, DateTime end, out bool someFlag) => {
        IEnumerable<SomeOtherClass> otherClasses = GenerateMockedData(start, end);
        //Assign something to someFlag, depending on start and end
        someFlag = true;
        return otherClasses;
    }));

The It.Ref<bool>.IsAny instructs the returns delegate to interact with the instance reference of the actual object that was passed into the mocked member. It.Ref<bool>.IsAny指示返回委托与传递到模拟成员中的实际对象的实例引用进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM