コード例 #1
0
        public async void SaveTest()
        {
            // Arrange
            var mockLogManager                = new Mock <ILogManager>();
            var mockContactRepository         = new Mock <IContactRepository>();
            var mockContact                   = new Mock <IContact>();
            var mockContactValidator          = new Mock <IContactValidator>();
            var mockAddressApplicationService = new Mock <IAddressApplicationService>();

            // Setup mock methods/properties
            mockContactRepository.Setup(x => x.SaveAsync(It.IsNotNull <IContact>()))
            .Returns(Task.FromResult(new SaveResponse <IContact> {
                Message = "Successful."
            }));
            mockContactValidator.Setup(x => x.ValidateAsync(It.IsNotNull <IContact>(), CancellationToken.None))
            .Returns(Task.FromResult(new ValidationResult()));

            // Act
            var sut = new ContactApplicationService(
                mockLogManager.Object, mockContactRepository.Object, mockContactValidator.Object, mockAddressApplicationService.Object);
            var response = await sut.SaveAsync(mockContact.Object);

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

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

            // Verify the application service is calling the validator
            mockContactValidator.Verify(x => x.ValidateAsync(It.IsNotNull <IContact>(), CancellationToken.None));
        }