简体   繁体   中英

How to mock indexer information for the returned object in NSubstitute

I want to mock indexer property return by the object. I am using NSubstitute for mocking. I have following scenario. In the sample I wanted to mock indexer property this[string symbolName] while returning from IFeedData.GetFeedData

public class FeedEntity
{
    private SymbolData _symbolData;
    public string Symbol { get; set; }

    public SymbolData this[string symbolName]
    {
        get //Search Data into Actual Feed and returns relevent data for Symbol
        {
            if (_symbolData != null)
            {
                _symbolData = new SymbolData//Just Created For Sample Purpose
                {
                    TraderName = "TestTrader",
                    BrokerName = "TestBroker"
                };
            }
            return _symbolData;
        }

        set
        {
            _symbolData = value;
        }
    }
}

public class SymbolData
{
    public string TraderName { get; set; }
    public string BrokerName { get; set; }
}

public interface IFeedData
{
    FeedEntity GetFeedData(string symbolName);
}

public class FeedClass : IFeedData
{
    public FeedEntity GetFeedData(string symbolName)
    {
        return new FeedEntity
        {
            Symbol = "TestSymbol"
        };
    }
}

Main Class that refer IFeedData -

public class FeedProcesser
{
        private IFeedData _feedService = new FeedClass();

        public FeedProcesser(IFeedData feedService)
        {
            _feedService = feedService;
        }

        public void ProcessFeedData(string symbol)
        {
           var feedRespnse= _feedService.GetFeedData(symbol);
           //do feed processing logic based on Trader and Broker Information
        }
}

Unit Test Class as -

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var iFeedData = Substitute.For<FeedClass>();
            iFeedData.GetFeedData(Arg.Any<string>()).Returns(x => new FeedEntity() { Symbol = "HHH" });
            var processer = new FeedProcesser(iFeedData);
            processer.ProcessFeedData("HHH");

        }
    }

Any help appreciated!

To mock FeedEntity we would need to make all its members virtual or extract an interface. We can then mock the indexer like this:

var feedEntity = Substitute.For<IFeedEntity>();
feedEntity["ABC"] = new SymbolData(...);
// or if it is a get-only indexer:
feedEntity["ABC"].Returns(new SymbolData(...));

If you just want to test FeedProcessor though we may not need to go to that level of detail. We could instead just substitute for the IFeedData .

[Test]
public void TestProcessing() {
    var feedData = Substitute.For<IFeedData>();
    feedData.GetFeedData("HHH").Returns(new FeedEntity(...)); 
    // Configure substitute to return a real FeedEntity. 
    // Alternatively, return a substitute IFeedEntity as described above
    var subject = new FeedProcessor(feedData);

    subject.ProcessFeedData();

    // assert correct processing  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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