当前位置: 首页>>代码示例>>C#>>正文


C# MockRepository.Setup方法代码示例

本文整理汇总了C#中Moq.MockRepository.Setup方法的典型用法代码示例。如果您正苦于以下问题:C# MockRepository.Setup方法的具体用法?C# MockRepository.Setup怎么用?C# MockRepository.Setup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Moq.MockRepository的用法示例。


在下文中一共展示了MockRepository.Setup方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: All_Provided_Text_Is_Added_When_The_Append_Option_Is_Selected

        public async Task All_Provided_Text_Is_Added_When_The_Append_Option_Is_Selected()
        {
            // Assort
            var store = new List<ForwardTextCandidate>();
            var repository = new MockRepository(MockBehavior.Default).Create<Repository<ForwardTextCandidate>>(new Mock<ISession>().Object);
            repository.Setup(x => x.Get()).Returns(store.AsQueryable());
            repository.Setup(x => x.Save(It.IsAny<IQueryable<ForwardTextCandidate>>())).Callback<IQueryable<ForwardTextCandidate>>(items => store.AddRange(items));

            var svc = new Mock<CsvFileTextService<ForwardTextCandidate>>();
            svc.Setup(x => x.Import(It.IsAny<string>(), MergeWithExistingRecordsPolicy.Append, repository.Object, NullEntityCreator, NullEntityLocator, NullEntityUpdater))
                .Callback<string, MergeWithExistingRecordsPolicy, IBaseRepository<ForwardTextCandidate>,  Func<string, ForwardTextCandidate>, Func<ForwardTextCandidate, string, bool>, Action<ForwardTextCandidate, string>>
                ((path, opc, rep, creator, locator, updater) =>
                {
                    var itemsToAdd = new List<ForwardTextCandidate>();
                    InputTextItems.ToList().ForEach(x => itemsToAdd.Add(new ForwardTextCandidate { Text = x }));
                    rep.Save(itemsToAdd.AsQueryable());
                })
                .Returns(Task.FromResult<ImportTextResult>(new ImportTextResult { Success = true, InsertedRecords = InputTextItems.Length }));
            
            // Act
            var result = await svc.Object.Import(string.Empty, MergeWithExistingRecordsPolicy.Append, repository.Object, NullEntityCreator, NullEntityLocator, NullEntityUpdater);

            // Assert
            Assert.IsTrue(result.Success);
            Assert.AreEqual(InputTextItems.Length, store.Count());

            var index = 0;
            store.ForEach(x => Assert.AreEqual(x.Text, InputTextItems[index++]));
        }
开发者ID:c0d3m0nky,项目名称:mty,代码行数:29,代码来源:CsvFileTextServiceTests.cs

示例2: CreateFakeRepository

        private static Mock<Repository<ForwardTextCandidate>> CreateFakeRepository(List<ForwardTextCandidate> store)
        {
            var repository = new MockRepository(MockBehavior.Default).Create<Repository<ForwardTextCandidate>>(new Mock<ISession>().Object);
            repository.Setup(x => x.Get()).Returns(store.AsQueryable());
            repository.Setup(x => x.Create(It.IsAny<ForwardTextCandidate>())).Callback<ForwardTextCandidate>(item => store.Add(item));
            repository.Setup(x => x.Delete(It.IsAny<ForwardTextCandidate>())).Callback<ForwardTextCandidate>(item => store.Remove(item));
            repository.Setup(x => x.Save(It.IsAny<ForwardTextCandidate>())).Callback<ForwardTextCandidate>(item =>
            {
                var rep = repository.Object;
                rep.Create(new ForwardTextCandidate { Text = item.Text, CreatedOn = DateTime.Now, LastUsedOn = DateTime.Now });
            });

            return repository;
        } 
开发者ID:c0d3m0nky,项目名称:mty,代码行数:14,代码来源:CsvFileTextServiceTests.cs


注:本文中的Moq.MockRepository.Setup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。