简体   繁体   English

模拟具有只读道具和内部ctor的返回类型的第三方界面

[英]Mock third-party interface with return type that has readonly props & internal ctor

I'm trying to mock a third-party interface that I'm using ( EventStore ClientAPI/IEventStoreConnection ), in particular this method: 我正在尝试模拟我正在使用的第三方接口( EventStore ClientAPI / IEventStoreConnection ),特别是此方法:

Task<StreamEventsSlice> ReadStreamEventsForwardAsync(string stream, long start, int count, bool resolveLinkTos, UserCredentials userCredentials = null);

The problem I'm having is that the return type StreamEventsSlice has readonly fields and an internal constructor ie 我遇到的问题是返回类型StreamEventsSlice具有readonly字段和internal构造函数即

public class StreamEventsSlice
{
    public readonly string Stream;
    //other similar fields

    internal StreamEventsSlice(string stream) //missing other fields
    {
        Stream = stream;
    }
}

In my test code I'm mocking the event store connection using Moq , setting up the ReadStreamEventsForwardAsyncMethod , and trying to set the return type like so: 在我的测试代码中,我使用Moq ReadStreamEventsForwardAsyncMethod事件存储连接,设置ReadStreamEventsForwardAsyncMethod ,并尝试设置返回类型,如下所示:

var connection = new Mock<IEventStoreConnection>();
connection.Setup(s => s.ReadStreamEventsForwardAsync(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<UserCredentials>())
    .ReturnsAsync(new StreamsEventSlice { props here });

But I can't set the properties, or call the constructor instead of that (I only actually need to set two properties) 但是我无法设置属性,或者调用构造函数而不是那个(我实际上只需要设置两个属性)

I've tried making a stub that extends the original class, and returning that instead. 我已经尝试制作一个扩展原始类的存根,然后返回它。 Although I can hide the readonly properties I get an error on the class saying 'StreamEventsSlice does not have a constructor that takes 0 arguments'. 虽然我可以隐藏readonly属性,但我在类中得到错误,说“StreamEventsSlice没有带0参数的构造函数”。 Giving it a constructor doesn't work as I can't call the base ctor since it's internal. 给它一个构造函数不起作用,因为我不能调用基本ctor,因为它是内部的。

How can I mock a method on an interface, when I can't instantiate the return type? 当我无法实例化返回类型时,如何在接口上模拟方法?

@MindSwipe linked two good answers, unfortunately I couldn't use this one for constructing an object as one of the parameters for the constructor was also set to internal. @MindSwipe链接了两个很好的答案,遗憾的是我不能用这个来构造一个对象,因为构造函数的一个参数也被设置为internal。 Instead I had to use this method , and use their other suggestion to set the properties. 相反,我必须使用此方法 ,并使用他们的其他建议来设置属性。

I kept most of mock setup code the same, other then adding and using the following method to instantiate StreamEventsSlice 我保留了大部分模拟设置代码,然后添加并使用以下方法实例化StreamEventsSlice

internal StreamEventsSlice GetStreamEventSlice(ResolvedEvent[] events = null, bool isEndOfStream = true)
{
    events = events ?? new ResolvedEvent[0];
    var type = typeof(StreamEventsSlice);
    var slice = (StreamEventsSlice) FormatterServices.GetUninitializedObject(type);
    type.GetField("Events").SetValue(slice, events);
    type.GetField("IsEndOfStream").SetValue(slice, isEndOfStream);

    return slice;
}

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

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