當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。