コード例 #1
0
        public async void SaveErrorTest()
        {
            // Arrange
            var mockLogManager                = new Mock <ILogManager>();
            var mockClientRepository          = new Mock <IClientRepository>();
            var mockClientValidator           = new Mock <IClientValidator>();
            var mockClient                    = new Mock <IClient>();
            var mockAddressApplicationService = new Mock <IAddressApplicationService>();
            var mockContactApplicationService = new Mock <IContactApplicationService>();


            // Setup mock methods/ properties
            mockClientRepository.Setup(x => x.SaveAsync(It.IsNotNull <IClient>()))
            .Throws(new Exception());
            mockClient.Setup(c => c.Id).Returns(It.IsAny <int>());
            mockClientValidator.Setup(x => x.ValidateAsync(It.IsNotNull <IClient>(), CancellationToken.None))
            .Returns(Task.FromResult(new ValidationResult()));

            // Act
            var sut = new ClientApplicationService(
                mockLogManager.Object, mockClientRepository.Object, mockClientValidator.Object, mockAddressApplicationService.Object, mockContactApplicationService.Object);
            var response = await sut.SaveAsync(mockClient.Object);

            // Assert
            response.IsSuccessful.Should().BeFalse();
            response.Errors.Count.Should().BeGreaterThan(0);
            response.Message.Should().NotBeNullOrEmpty();

            // Verify the application service is calling the correct repository method.
            mockClientRepository.Verify(x => x.SaveAsync(It.IsAny <IClient>()));

            // Verify the application service is logging the error.
            mockLogManager.Verify(x => x.LogError(It.IsAny <Exception>(), It.IsAny <string>()));
        }
コード例 #2
0
        public async void SaveTest()
        {
            // Arrange
            var mockLogManager                = new Mock <ILogManager>();
            var mockClientRepository          = new Mock <IClientRepository>();
            var mockClientValidator           = new Mock <IClientValidator>();
            var mockClient                    = new Mock <IClient>();
            var mockAddressApplicationService = new Mock <IAddressApplicationService>();
            var mockContactApplicationService = new Mock <IContactApplicationService>();

            // Setup mock methods/ properties
            mockClientRepository.Setup(x => x.SaveAsync(It.IsAny <IClient>()))
            .Returns(Task.FromResult(new SaveResponse <IClient> {
                Message = "Successful."
            }));
            mockClientValidator.Setup(x => x.ValidateAsync(It.IsNotNull <IClient>(), CancellationToken.None))
            .Returns(Task.FromResult(new ValidationResult()));
            mockAddressApplicationService.Setup(x => x.SaveAsync(It.IsAny <IAddress>()))
            .Returns(Task.FromResult(new SaveResponse <IAddress> {
                Message = "Successful."
            }));
            mockContactApplicationService.Setup(x => x.SaveAllAsync(It.IsAny <IReadOnlyList <IContact> >()))
            .Returns(Task.FromResult(new SaveResponse <IReadOnlyList <IContact> > {
                Message = "Successful."
            }));

            // Act
            var sut = new ClientApplicationService(
                mockLogManager.Object, mockClientRepository.Object, mockClientValidator.Object, mockAddressApplicationService.Object, mockContactApplicationService.Object);
            var response = await sut.SaveAsync(mockClient.Object);

            // Verify correct response
            response.IsSuccessful.Should().BeTrue();
            response.Errors.Count.Should().Be(0);
            response.Message.Should().NotBeNullOrEmpty();

            // Verify the application service is calling dependent methods
            mockClientRepository.Verify(x => x.SaveAsync(It.IsAny <IClient>()));
            mockClientValidator.Verify(x => x.ValidateAsync(It.IsNotNull <IClient>(), CancellationToken.None));
            mockAddressApplicationService.Verify(x => x.SaveAsync(It.IsAny <IAddress>()));
            mockContactApplicationService.Verify(x => x.SaveAllAsync(It.IsAny <IReadOnlyList <IContact> >()));
        }