简体   繁体   English

Rhino Mock,Mock接口列表

[英]Rhino Mock, Mock list of interface

How can i Mock list of object in for loop 我如何在for循环中模拟对象列表

Here is the code:` 这是代码:

 public class SearchResult<T>
    {


        private readonly ISearcher<T> _searcher;
        private readonly IList<ISearchConfigurator> _configurators; 


        public SearchResult(ISearcher<T> searcher, IList<ISearchConfigurator> configurators)
        {
            _searcher = searcher;
            _configurators = configurators;
        }


        public DomainSearchResult<T> FindInAllDomains()
        {
            DomainSearchResult domainSearchResults = new DomainSearchResult<T>();
            foreach (var configurator in _configurators)
            {
                IList<T> results = _searcher.SearchAll(configurator);
                domainSearchResults.Results.Add(_configurator.DomainName, results);
            }
            return domainSearchResults;
        }
    }`

the property result is declared in the DomainSearchResult class : 属性结果在DomainSearchResult类中声明:

IDictionary<string,IList<T>> Results { get; set; }

then i tried the following: 然后我尝试了以下方法:

        [Test]
    public void FindInAllDomains_ReturnsAllRecord()
    {
        //Arrange
        var configuratorMock = MockRepository.GenerateStub<IList<ISearchConfigurator>>();
        var searchMock = MockRepository.GenerateMock<ISearcher<NativeDs>>();
        var searchRestul = new SearchResult<NativeDs>(searchMock, configuratorMock);

        //Act
        searchRestul.FindInAllDomains(); // calling test fail here

        //Assert
        searchMock.AssertWasCalled(x => x.SearchAll(null), opt => opt.IgnoreArguments());
    }

Error is: System.NullReferenceException : Object reference not set to an instance of an object. 错误是:System.NullReferenceException:对象引用未设置为对象的实例。

Try this... 尝试这个...

var list = new List<ISearchConfigurator>
          (from x in Enumerable.Range(1, 100)
           select MockRepository.GenerateMock<ISearchConfigurator>()
          );

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

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