Rhino Mock,Mock界面列表

时间:2013-04-14 15:29:46

标签: c# unit-testing mocking

我如何在for循环中模拟对象列表

这是代码:`

 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;
        }
    }`

属性结果在DomainSearchResult类中声明:

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

然后我尝试了以下内容:

        [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());
    }

错误是:     System.NullReferenceException:未将对象引用设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

试试这个......

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