protected void SetupMultiple <T>(IEnumerable <T> values) where T : BaseDto { RepositoryMock.Setup(r => r.GetAllAsync <T>()) .ReturnsAsync(values) .Verifiable(); }
public async Task GetCallToActionOptionsFromTypeAsync_GoodFlow( [CallToActionOptionDataSource(30)] IEnumerable <CallToActionOption> options) { // Arrange RepositoryMock .Setup(repository => repository.GetCallToActionOptionsFromTypeAsync(It.IsAny <string>())) .ReturnsAsync(options); // Act IEnumerable <CallToActionOption> actualOptions = await Service.GetCallToActionOptionsFromTypeAsync(It.IsAny <string>()); Action act = () => RepositoryMock.Verify(repository => repository.GetCallToActionOptionsFromTypeAsync(It.IsAny <string>()), Times.Once()); // Assert act.Should() .NotThrow(); actualOptions .Should() .Contain(options); actualOptions.Count() .Should() .Be(30); }
public void VmShouldNotBeClosedHasChangesResultCancel() { RepositoryMock .Setup(repository => repository.SaveAsync(It.IsAny <IEnumerable <IEntityStateEntry> >(), It.IsAny <CancellationToken>())) .Returns(() => Empty.TrueTask); MessagePresenterMock .Setup(box => box.ShowAsync(It.IsAny <string>(), It.IsAny <string>(), MessageButton.YesNoCancel, MessageImage.Question, It.IsAny <MessageResult>(), It.IsAny <IDataContext>())) .Returns(Task.FromResult(MessageResult.Cancel)); SetupEditableWrapper(true); InitializeProductRepository(); var viewModel = GetViewModel <ProductWorkspaceViewModel>(); viewModel.AddProductCommand.Execute(null); Assert.IsTrue(viewModel.HasChanges); Assert.IsFalse(viewModel.CloseAsync(null).Result); MessagePresenterMock .Verify(model => model.ShowAsync(It.IsAny <string>(), It.IsAny <string>(), MessageButton.YesNoCancel, MessageImage.Question, It.IsAny <MessageResult>(), It.IsAny <IDataContext>()), Times.Once); RepositoryMock .Verify(repository => repository.SaveAsync(It.IsAny <IEnumerable <IEntityStateEntry> >(), It.IsAny <CancellationToken>()), Times.Never); }
public VelocityTests() { CalendarMock = new(); RepositoryMock = new(); Velocity = new(CalendarMock.Object, RepositoryMock.Object); Swimlanes = new[] { new Swimlane(0, "Documents", "docs"), new Swimlane(1, "Tech Debts", "tech debts"), new Swimlane(2, "Bug Fixes", "bugs") }; RepositoryMock .Setup(repository => repository.GetProject(1)) .Returns(new Project( id: 0, name: "whipping-boy", beginSprintsAt: new(2021, 01, 01), gitHubSettings: new( installationId: 123456, repositoryId: 123457, swimlanes: Swimlanes, allowedProjectIds: null))); RepositoryMock .Setup(repository => repository.GetProjectSprints(1)) .Returns(new[] { new Sprint(0, new(), 7) });
protected void SetupDelete <T>(Func <Expression, bool> predicateCheck) where T : BaseDto { RepositoryMock.Setup(r => r.DeleteAsync(It.Is <Expression <Func <T, bool> > >(e => predicateCheck(e)))) .ReturnsAsync(true) .Verifiable(); }
public async Task ShouldKeepUpdatingWorkitemsIfOneUpdateFails() { var workitems = Builder <WorkItemViewModel> .CreateListOfSize(8) .All() .With((w, idx) => w.WorkItemId = idx) .Build(); var member = Builder <TeamMemberViewModel> .CreateNew().Build(); var memberDto = Builder <TeamMember> .CreateNew() .With(m => m.RelatedWorkItems, Enumerable.Range(20, 3).ToArray()) .Build(); var combinedIds = workitems.Select(w => w.WorkItemId).Union(memberDto.RelatedWorkItems).ToArray(); SetupSingle(memberDto); SetupCreateOrUpdateIfManual <WorkItem>(_ => true, i => i.WorkItemId == 2) .Throws <Exception>(); SetupCreateOrUpdateIfManual <WorkItem>(_ => true, i => i.WorkItemId != 2) .ReturnsAsync(true); RepositoryMock.Setup(r => r.UpdateFieldValue(It.IsAny <TeamMember>(), It.IsAny <Expression <Func <TeamMember, int[]> > >(), It.Is <int[]>(v => CheckIfRelatedWorkitemsAreCorrect(v, combinedIds)))) .Returns(Task.CompletedTask) .Verifiable(); RepositoryMock.Setup(r => r.UpdateFieldValue(It.IsAny <TeamMember>(), It.IsAny <Expression <Func <TeamMember, DateTime?> > >(), It.IsAny <DateTime>())) .Returns(Task.CompletedTask) .Verifiable(); await _handler.Handle(new SaveWorkItemsForUser(workitems, member)); RepositoryMock.Verify(r => r.UpdateFieldValue(It.IsAny <TeamMember>(), It.IsAny <Expression <Func <TeamMember, int[]> > >(), It.IsAny <int[]>()), Times.Once()); RepositoryMock.Verify(r => r.UpdateFieldValue(It.IsAny <TeamMember>(), It.IsAny <Expression <Func <TeamMember, DateTime?> > >(), It.IsAny <DateTime>()), Times.Once()); RepositoryMock.Verify(r => r.CreateOrUpdateIfAsync(It.IsAny <Expression <Func <WorkItem, bool> > >(), It.IsAny <WorkItem>()), Times.Exactly(workitems.Count)); }
protected void SetupMultiple <T>(Func <Expression, bool> predicateCheck, IEnumerable <T> values) where T : BaseDto { RepositoryMock.Setup(r => r.GetAsync(It.Is <Expression <Func <T, bool> > >(e => predicateCheck(e)))) .ReturnsAsync(values) .Verifiable(); }
public void RemoveCmdShouldAddChangesResultYes() { var items = new List <OrderModel> { new OrderModel { Id = Guid.NewGuid() } }; IList <OrderProductModel> links = new List <OrderProductModel>(); RepositoryMock .Setup(repository => repository.GetProductsByOrderAsync(items[0].Id, It.IsAny <CancellationToken>())) .Returns(Task.FromResult(links)); MessagePresenterMock .Setup(box => box.ShowAsync(It.IsAny <string>(), It.IsAny <string>(), MessageButton.YesNo, MessageImage.Question, It.IsAny <MessageResult>(), It.IsAny <IDataContext>())) .Returns(Task.FromResult(MessageResult.Yes)); InitializeRepository(items); var viewModel = GetViewModel <OrderWorkspaceViewModel>(); viewModel.GridViewModel.SelectedItem = items[0]; viewModel.RemoveOrderCommand.Execute(null); Assert.IsTrue(viewModel.HasChanges); Assert.AreEqual(viewModel.GridViewModel.OriginalItemsSource.Count, 0); MessagePresenterMock.Verify( model => model.ShowAsync(It.IsAny <string>(), It.IsAny <string>(), MessageButton.YesNo, MessageImage.Question, It.IsAny <MessageResult>(), It.IsAny <IDataContext>()), Times.Once); RepositoryMock.Verify( repository => repository.GetProductsByOrderAsync(items[0].Id, It.IsAny <CancellationToken>()), Times.Once); }
protected void SetupMultipleWithPredicate <T>(IEnumerable <T> values) where T : BaseDto { RepositoryMock.Setup(r => r.GetAsync(It.IsAny <Expression <Func <T, bool> > >())) .Returns <Expression <Func <T, bool> > >(e => Task.FromResult(values.Where(e.Compile()))) .Verifiable(); }
public async Task IsInCodeReviewReturnTrueIfActivePullRequests() { var relations = Builder <ViewModels.Types.WorkItemRelation> .CreateListOfSize(2) .All() .With(r => r.RelationType, "ArtifactLink") .TheFirst(1) .With(r => r.Url, new Uri("vstfs:///Git/PullRequestId/bla%2ffoo%2f123456789")) .TheNext(1) .With(r => r.Url, new Uri("vstfs:///Git/PullRequestId/bla%2ffoo%2f987654321")) .Build(); var item = Builder <WorkItemViewModel> .CreateNew() .With(w => w.Fields, new Dictionary <string, string> { [WorkItemStateField] = WorkItemStateActive, [WorkItemTagsField] = string.Empty }) .With(w => w.Relations, relations) .Build(); var prs = Builder <PullRequest> .CreateListOfSize(2) .All() .With(p => p.State, PullRequestState.Active) .Build(); RepositoryMock.Setup(r => r.GetAsync(It.IsAny <Expression <Func <PullRequest, bool> > >())) .ReturnsAsync(prs); var isInCodeReview = await _dataSource.IsInCodeReview(item); isInCodeReview.Should().BeTrue(); }
public void EditCmdShouldAddChangesResultTrue() { var items = new List <OrderModel> { new OrderModel { Id = Guid.NewGuid() } }; IList <OrderProductModel> links = new List <OrderProductModel>(); RepositoryMock .Setup(repository => repository.GetProductsByOrderAsync(items[0].Id, It.IsAny <CancellationToken>())) .Returns(Task.FromResult(links)); var wrapper = SetupEditableWrapper(true); InitializeRepository(items); var viewModel = GetViewModel <OrderWorkspaceViewModel>(); viewModel.GridViewModel.SelectedItem = items[0]; viewModel.EditOrderCommand.Execute(null); Assert.IsTrue(viewModel.HasChanges); ViewModelPresenterMock.Verify(model => model.ShowAsync(wrapper.Object, It.IsAny <IDataContext>()), Times.Once); RepositoryMock.Verify( repository => repository.GetProductsByOrderAsync(items[0].Id, It.IsAny <CancellationToken>()), Times.Once); }
private void SetupGetFieldValue <TType, TProjection>(Guid id, TProjection value) where TType : BaseDto { RepositoryMock.Setup(r => r.GetFieldValueAsync(id, It.IsAny <Expression <Func <TType, TProjection> > >())) .ReturnsAsync(value) .Verifiable(); }
public async Task ShouldReturnCorrectReportType() { const int expectedNumberOfReports = 5; var id = Guid.NewGuid(); var individualReports = Builder <AggregatedWorkitemsETAReport.IndividualETAReport> .CreateListOfSize(expectedNumberOfReports).Build(); var report = new AggregatedWorkitemsETAReport(expectedNumberOfReports); report.IndividualReports.AddRange(individualReports); RepositoryMock.Setup(r => r.GetFieldValueAsync(It.IsAny <Expression <Func <ReportResult, bool> > >(), It.IsAny <Expression <Func <ReportResult, string> > >())) .ReturnsAsync(Constants.ETAReportType.ToLower()) .Verifiable(); RepositoryMock.Setup(r => r.GetSingleAsync(id, It.Is <Type>(t => t == typeof(AggregatedWorkitemsETAReport)))) .ReturnsAsync(report) .Verifiable(); var result = await _handler.Handle(new GetReportById(id)); result.Should().NotBeNull(); result.Should().BeOfType <AggregatedWorkitemsETAReportViewModel>(); result.As <AggregatedWorkitemsETAReportViewModel>().IndividualReports.Should().HaveCount(expectedNumberOfReports); RepositoryMock.Verify(); }
public void GetAsync_ThrowsValidationException() { RepositoryMock.Setup(repo => repo.GetAsync(It.IsAny <int>())) .Returns(GetExceptionTest()); Assert.ThrowsAsync <ValidationException>(async() => await HomeworkService.GetAsync(null)); Assert.ThrowsAsync <ValidationException>(async() => await HomeworkService.GetAsync(It.IsAny <int>())); }
protected void SetupCreateOrUpdate <T, TModel>(TModel model) where T : BaseDto { var value = Mapper.Map <T>(model).ToExpectedObject(); RepositoryMock.Setup(r => r.CreateOrUpdateAsync(It.Is <T>(v => value.Equals(v)))) .ReturnsAsync(true) .Verifiable(); }
public BurndownTests() { CalendarMock = new(); RepositoryMock = new(); Burndown = new(CalendarMock.Object, RepositoryMock.Object); RepositoryMock .Setup(repository => repository.GetProjectSprints(1)) .Returns(new[] { new Sprint(0, new(), 7) });
public void GetComponentInterface_ComponentExists_ShouldThrowNotFoundException() { //Arrange RepositoryMock.Setup(x => x.Query(It.IsAny <PersistenceAwareSpecification <ComponentInterface> >())) .Returns(new List <ComponentInterface>().AsAsyncQueryable()); //Assert UtilsAssert.AssertThrowsAggregateException <NotFoundException, ComponentInterfaceVO>( () => Service.GetComponentInterface(Guid.NewGuid()).Result); }
protected ISetup <IRepository, Task <bool> > SetupCreateOrUpdateIfManual <T>(Func <Expression, bool> predicate, Expression <Func <T, bool> > itemPredicate = null) where T : BaseDto { if (itemPredicate == null) { itemPredicate = _ => true; } return(RepositoryMock.Setup(r => r.CreateOrUpdateIfAsync(It.Is <Expression <Func <T, bool> > >(p => predicate(p)), It.Is(itemPredicate)))); }
public async Task ShouldReturnNullIfUnsupportedReportType() { RepositoryMock.Setup(r => r.GetFieldValueAsync(It.IsAny <Expression <Func <ReportResult, bool> > >(), It.IsAny <Expression <Func <ReportResult, string> > >())) .ReturnsAsync("FakeReportType") .Verifiable(); var result = await _handler.Handle(new GetReportById(Guid.NewGuid())); result.Should().BeNull(); RepositoryMock.Verify(); }
public void ShouldSuppressExceptions() { RepositoryMock.Setup(r => r.UpdateFieldValue( It.IsAny <Expression <Func <JobLog, bool> > >(), It.IsAny <Expression <Func <JobLog, JobExecutionState> > >(), It.IsAny <JobExecutionState>())) .Throws <Exception>(); _handler.Awaiting(h => h.Handle(new AbortActiveJobs())) .Should().NotThrow(); }
private void SetupProjectAndOrganization() { var org = Builder <VstsOrganization> .CreateNew() .Build(); var project = Builder <Project> .CreateNew() .With(p => p.Organization, org.Id) .Build(); RepositoryMock.Setup(r => r.GetSingle(It.IsAny <Expression <Func <Project, bool> > >())).Returns(project); RepositoryMock.Setup(r => r.GetSingle(It.IsAny <Expression <Func <VstsOrganization, bool> > >())).Returns(org); }
private void InitializeProductRepository(IList <ProductModel> productModels = null) { if (productModels == null) { productModels = new ProductModel[0]; } Task <IList <ProductModel> > result = Task.FromResult(productModels); RepositoryMock .Setup(repository => repository.GetProductsAsync(It.IsAny <CancellationToken>())) .Returns(() => result); }
protected void SetupCreateOrUpdate <T>(Expression <Func <T, bool> > itemPredicate = null) where T : BaseDto { if (itemPredicate == null) { itemPredicate = _ => true; } RepositoryMock.Setup(r => r.CreateOrUpdateAsync(It.Is(itemPredicate))) .ReturnsAsync(true) .Verifiable(); }
protected void SetupSingle <T>(T value, Func <Guid, bool> predicate = null) where T : BaseDto { if (predicate == null) { predicate = _ => true; } RepositoryMock.Setup(r => r.GetSingleAsync <T>(It.Is <Guid>(e => predicate(e)))) .ReturnsAsync(value) .Verifiable(); }
public async Task GetAllAsync([RoleDataSource(10)] List <Role> roles) { RepositoryMock.Setup(repository => repository.GetAllAsync()) .Returns(Task.FromResult(roles)); List <Role> retrievedRoles = await Service.GetAllAsync(); Assert.DoesNotThrow(() => { RepositoryMock.Verify(repository => repository.GetAllAsync(), Times.Once); }); Assert.AreEqual(roles, retrievedRoles); Assert.AreEqual(10, retrievedRoles.Count); }
private List <ComponentInterface> FillRepositoryWithFakes(int fakesCount) { var intsList = new List <ComponentInterface>(); for (var i = 0; i < fakesCount; i++) { intsList.Add(DomainObjectsCreator.CreateInterface(i)); } RepositoryMock.Setup(x => x.Query(It.IsAny <PersistenceAwareSpecification <ComponentInterface> >())) .Returns(intsList.AsAsyncQueryable()); return(intsList); }
public void UpdateAsync_ThrowsValidationException() { RepositoryMock.Setup(repo => repo.GetAsync(It.IsAny <int>())) .Returns(GetExceptionTest()); Assert.ThrowsAsync <ValidationException>(async() => await HomeworkService .UpdateAsync(_homeworkDTO)); Assert.ThrowsAsync <ValidationException>(async() => await HomeworkService .UpdateAsync(_homeworkDTOForPresenceException)); Assert.ThrowsAsync <ValidationException>(async() => await HomeworkService .UpdateAsync(_homeworkDTOForNotPresenceException)); }
protected sealed override void Initialize() { DataSourceMock = new Mock <IDataSource>(MockBehavior.Strict); var ds = DataSourceMock.Object; DataSourceProviderMock = new Mock <IIndex <string, IDataSource> >(MockBehavior.Strict); DataSourceProviderMock.Setup(p => p.TryGetValue(DataSourceType, out ds)).Returns(true); RepositoryMock.Setup(r => r.GetFieldValueAsync(It.IsAny <Expression <Func <Profile, bool> > >(), It.IsAny <Expression <Func <Profile, string> > >())) .ReturnsAsync(DataSourceType) .Verifiable(); Handler = InitializeHandler(); }
public async Task RemoveUserAsync_GoodFlow() { int userId = 1; RepositoryMock.Setup( repository => repository.RemoveUserAsync(userId)); await Service.RemoveUserAsync(userId); Assert.DoesNotThrow(() => { RepositoryMock.Verify(repository => repository.RemoveUserAsync(userId), Times.Once); }); }
public void ChassiFind_Not_Found_By_The_Repository(string chassi) { // Arrange RepositoryMock.Setup(r => r.ChassiFind(It.IsAny <string>())).Returns <Vehicle>(null); // Act var result = Service.ChassiFind(chassi); // Assert Assert.IsNull(result, "Veículo não encontrado."); RepositoryMock.Verify(r => r.ChassiFind(It.IsAny <string>()), Times.Once(), "Realizou a pesquisa no repositório."); RepositoryMock.Verify(r => r.ChassiFind(chassi), Times.Once(), "Pesquisou apenas o chassi solicitado."); }