Esempio n. 1
0
        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());
        }
Esempio n. 2
0
        public async Task HandleAsyncSuccessTest()
        {
            var request = new CreateTodoItemRequest();

            var expectedResponse = CreateExpectedResponse();

            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 mockRulesEngine = new Mock <IRulesEngine>();

            // Setup generic mock setups first and then more specific.
            SetupMockProcessAsyncWithSaveRules(mockRulesEngine, request, saveRules,
                                               callbackResponse => ProcessCallBackResponseToCloneTodoItem(callbackResponse, expectedResponse));
            SetupMockProcessAsyncWithCreateRules(mockRulesEngine, request, createRules,
                                                 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 mockCreateTodoItemRepository = new Mock <ICreateTodoItemRepository>();

            mockCreateTodoItemRepository.Setup(repository =>
                                               repository.SaveAsync(It.Is <Response <TodoItemEntity> >(response => VerifyTodoItem.AreEqualResponse(expectedResponse, response))));

            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(), 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), Times.Once);

            mockCreateTodoItemRepository.Verify(repository =>
                                                repository.SaveAsync(It.Is <Response <TodoItemEntity> >(response => VerifyTodoItem.AreEqualResponse(expectedResponse, response))), Times.Once);

            mockCreateRulesFactory.VerifyNoOtherCalls();
            mockSaveRulesFactory.VerifyNoOtherCalls();
            mockRulesEngine.VerifyNoOtherCalls();
            mockCreateTodoItemRepository.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.IsNotNull(actualResponse.Item);
            Assert.IsFalse(actualResponse.HasErrors());
            Assert.AreEqual(0, actualResponse.Notifications.Count);
            VerifyTodoItem.AssertAreEqualResponse(expectedResponse, actualResponse);
        }