public async Task HandleAsyncHasErrorTest(bool createRulesHasError) { var request = new CreateTodoItemRequest(); var createRules = RuleFactory.CreateEmptyListOfCreateRules().ToList(); createRules.Add(RuleFactory.CreateMockCreateRule()); var mockCreateRulesFactory = new Mock <IRulesFactory <CreateTodoItemRequest, Response <TodoItemEntity> > >(); mockCreateRulesFactory.Setup(rulesFactory => rulesFactory.Create()).Returns(createRules); var saveRules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); saveRules.Add(RuleFactory.CreateMockSaveRule()); var mockSaveRulesFactory = new Mock <IRulesFactory <Request <TodoItem>, Response <TodoItemEntity> > >(); mockSaveRulesFactory.Setup(rulesFactory => rulesFactory.Create()).Returns(saveRules); var errorNotification = Notification.CreateError("1", "error"); var mockRulesEngine = new Mock <IRulesEngine>(); // Setup generic mock setups first and then more specific. SetupMockProcessAsyncWithSaveRules(mockRulesEngine, request, saveRules, callbackResponse => ProcessCallBackResponseToAddNotification(!createRulesHasError, callbackResponse, errorNotification)); SetupMockProcessAsyncWithCreateRules(mockRulesEngine, request, createRules, callbackResponse => ProcessCallBackResponseToAddNotification(createRulesHasError, callbackResponse, errorNotification)); var mockCreateTodoItemRepository = new Mock <ICreateTodoItemRepository>(); var mockLogger = new Mock <ILogger <CreateTodoItemRequestHandler> >(); var handler = new CreateTodoItemRequestHandler(mockRulesEngine.Object, mockCreateRulesFactory.Object, mockSaveRulesFactory.Object, mockCreateTodoItemRepository.Object, mockLogger.Object); var actualResponse = await handler.HandleAsync(request); mockCreateRulesFactory.Verify(rulesFactory => rulesFactory.Create(), Times.Once); mockSaveRulesFactory.Verify(rulesFactory => rulesFactory.Create(), createRulesHasError ? Times.Never : Times.Once); mockRulesEngine.Verify(rulesEngine => rulesEngine.ProcessAsync(request, It.IsAny <Response <TodoItemEntity> >(), createRules), Times.Once); mockRulesEngine.Verify(rulesEngine => rulesEngine.ProcessAsync(request, It.IsAny <Response <TodoItemEntity> >(), saveRules), createRulesHasError ? Times.Never : Times.Once); mockCreateRulesFactory.VerifyNoOtherCalls(); mockSaveRulesFactory.VerifyNoOtherCalls(); mockRulesEngine.VerifyNoOtherCalls(); mockCreateTodoItemRepository.VerifyNoOtherCalls(); mockLogger.VerifyNoOtherCalls(); Assert.IsNotNull(actualResponse); Assert.IsNull(actualResponse.Item); Assert.AreEqual(1, actualResponse.Notifications.Count); Assert.AreSame(errorNotification, actualResponse.Notifications.Single()); }
private static IEnumerable <object[]> SetupProcessRuleAsyncSkipped() { var rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); yield return(new object[] { "Rules: 0 of 3 required rules, 0 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); yield return(new object[] { "Rules: 0 of 3 required rules, 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <RequestRule>()); yield return(new object[] { "Rules: 1 of 3 required rules (RequestRule), 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <ResponseRule>()); yield return(new object[] { "Rules: 1 of 3 required rules (ResponseRule), 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <TodoItemPriorityRule>()); yield return(new object[] { "Rules: 1 of 3 required rules (TodoItemPriorityRule), 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <RequestRule>()); rules.Add(RuleFactory.CreatePassedRule <ResponseRule>()); yield return(new object[] { "Rules: 2 of 3 required rules (RequestRule, ResponseRule), 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <RequestRule>()); rules.Add(RuleFactory.CreatePassedRule <TodoItemPriorityRule>()); yield return(new object[] { "Rules: 2 of 3 required rules (RequestRule, TodoItemPriorityRule), 1 MockRule", rules }); rules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); rules.Add(RuleFactory.CreateMockSaveRule()); rules.Add(RuleFactory.CreatePassedRule <ResponseRule>()); rules.Add(RuleFactory.CreatePassedRule <TodoItemPriorityRule>()); yield return(new object[] { "Rules: 2 of 3 required rules (ResponseRule, TodoItemPriorityRule), 1 MockRule", rules }); }
public async Task HandleAsyncSuccessTest() { var request = new UpdateTodoItemRequest(); var expectedResponse = CreateExpectedResponse(); var updateRules = RuleFactory.CreateEmptyListOfUpdateRules().ToList(); updateRules.Add(RuleFactory.CreateMockUpdateRule()); var mockUpdateRulesFactory = new Mock <IRulesFactory <UpdateTodoItemRequest, Response <TodoItemEntity> > >(); mockUpdateRulesFactory.Setup(rulesFactory => rulesFactory.Create()).Returns(updateRules); var saveRules = RuleFactory.CreateEmptyListOfSaveRules().ToList(); saveRules.Add(RuleFactory.CreateMockSaveRule()); var mockSaveRulesFactory = new Mock <IRulesFactory <Request <TodoItem>, Response <TodoItemEntity> > >(); mockSaveRulesFactory.Setup(rulesFactory => rulesFactory.Create()).Returns(saveRules); var mockRulesEngine = new Mock <IRulesEngine>(); // Setup generic mock setups first and then more specific. SetupMockProcessAsyncWithSaveRules(mockRulesEngine, request, saveRules, callbackResponse => ProcessCallBackResponseToCloneTodoItem(callbackResponse, expectedResponse)); SetupMockProcessAsyncWithUpdateRules(mockRulesEngine, request, updateRules, callbackResponse => ProcessCallBackResponseToCloneTodoItem(callbackResponse, expectedResponse)); mockRulesEngine.Setup(rulesEngine => rulesEngine.ProcessAsync(request, It.IsAny <Response <TodoItemEntity> >(), saveRules)) .Callback <Request <TodoItem>, Response <TodoItemEntity>, IEnumerable <IBaseRule <Request <TodoItem>, Response <TodoItemEntity> > > > ((callbackRequest, callbackResponse, callbackRules) => { callbackResponse.Item = expectedResponse.Clone().Item; }); var mockUpdateTodoItemRepository = new Mock <IUpdateTodoItemRepository>(); mockUpdateTodoItemRepository.Setup(repository => repository.SaveAsync(It.Is <Response <TodoItemEntity> >(response => VerifyTodoItem.AreEqualResponse(expectedResponse, response)))); var mockLogger = new Mock <ILogger <UpdateTodoItemRequestHandler> >(); var handler = new UpdateTodoItemRequestHandler(mockRulesEngine.Object, mockUpdateRulesFactory.Object, mockSaveRulesFactory.Object, mockUpdateTodoItemRepository.Object, mockLogger.Object); var actualResponse = await handler.HandleAsync(request); mockUpdateRulesFactory.Verify(rulesFactory => rulesFactory.Create(), Times.Once); mockSaveRulesFactory.Verify(rulesFactory => rulesFactory.Create(), Times.Once); mockRulesEngine.Verify(rulesEngine => rulesEngine.ProcessAsync(request, It.IsAny <Response <TodoItemEntity> >(), updateRules), Times.Once); mockRulesEngine.Verify(rulesEngine => rulesEngine.ProcessAsync(request, It.IsAny <Response <TodoItemEntity> >(), saveRules), Times.Once); mockUpdateTodoItemRepository.Verify(repository => repository.SaveAsync(It.Is <Response <TodoItemEntity> >(response => VerifyTodoItem.AreEqualResponse(expectedResponse, response))), Times.Once); mockUpdateRulesFactory.VerifyNoOtherCalls(); mockSaveRulesFactory.VerifyNoOtherCalls(); mockRulesEngine.VerifyNoOtherCalls(); mockUpdateTodoItemRepository.VerifyNoOtherCalls(); // Test critical behavior. If the logging here is critical, then verify the calls. // See examples of ILogger testing in other parts of this app. mockLogger.VerifyAll(); Assert.IsNotNull(actualResponse); Assert.IsFalse(actualResponse.HasErrors()); Assert.AreEqual(0, actualResponse.Notifications.Count); }