public async Task ProcessRuleAsyncPass(string scenario, object expectedItem) { var expectedTodoItemEntity = (TodoItemEntity)expectedItem; var request = CreateRequest((int)expectedTodoItemEntity.Id); var response = CreateResponse(); var rule = new ResponseRule(); await rule.ProcessAsync(request, response, RuleFactory.CreateEmptyListOfSaveRules()); VerifyTodoItem.AssertTodoItem(expectedTodoItemEntity, response.Item); Assert.AreEqual(RuleStatusType.Passed, rule.Status); Assert.IsTrue(rule.ContinueProcessing); Assert.AreEqual(0, response.Notifications.Count); }
public async Task SaveAsyncTest() { // Arrange var todoItemEntity = TodoItemFactory.CreateTodoItemEntities(1).Single(); var response = new Response <TodoItemEntity> { Item = todoItemEntity }; // Act var repository = new CreateTodoItemRepository(Context); await repository.SaveAsync(response); // Assert var actualTodoItemEntity = await GetTodoItem(1); VerifyTodoItem.AssertTodoItem(response.Item, actualTodoItemEntity); }
public async Task HandleAsyncTest(int totalItems, int requestId) { var todoItemEntities = await PopulateTodoItems(totalItems); var expectedTodoItemEntity = todoItemEntities.SingleOrDefault(item => item.Id == requestId); var handler = new GetTodoItemRequestHandler(Context); var request = new GetTodoItemRequest { Id = requestId }; var response = await handler.HandleAsync(request); Assert.IsNotNull(response); Assert.AreEqual(0, response.Notifications.Count); VerifyTodoItem.AssertTodoItem(expectedTodoItemEntity, response.Item); }
public async Task HandleAsyncTest(int totalItems) { var todoItemEntities = await PopulateTodoItems(totalItems); var handler = new GetTodoItemsRequestHandler(Context); var request = new GetTodoItemsRequest(); var response = await handler.HandleAsync(request); Assert.IsNotNull(response?.Item); Assert.AreEqual(0, response.Notifications.Count); Assert.AreEqual(todoItemEntities.Count, response.Item.Count); foreach (var todoItemEntity in todoItemEntities) { var todoItem = response.Item.SingleOrDefault(item => item.Id == todoItemEntity.Id); VerifyTodoItem.AssertTodoItem(todoItemEntity, todoItem); } }
public async Task SaveAsyncSuccessTest() { // Arrange var todoItemEntities = await PopulateTodoItems(1); var expectedResponse = new Response <TodoItemEntity> { Item = todoItemEntities.Single().Clone() }; expectedResponse.Item.Description = "Updated"; // The response is changed in the SaveAsync, so don't use the same expected response as the one used in the method. var response = expectedResponse.Clone(); // Act var repository = new UpdateTodoItemRepository(Context); await repository.SaveAsync(response); // Assert var actualTodoItemEntity = await GetTodoItem(1); VerifyTodoItem.AssertTodoItem(expectedResponse.Item, actualTodoItemEntity); VerifyTodoItem.AssertTodoItem(expectedResponse.Item, response.Item); }
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); }