コード例 #1
0
        public async void GetTest()
        {
            // 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
            mockContact.Setup(c => c.Id).Returns(It.IsAny <int>());
            mockContactRepository.Setup(x => x.GetByIdAsync(It.IsAny <int>()))
            .Returns(Task.FromResult(new GetResponse <IContact> {
                Message = "Successful."
            }));

            // Act
            var sut = new ContactApplicationService(
                mockLogManager.Object, mockContactRepository.Object, mockContactValidator.Object, mockAddressApplicationService.Object);
            var response = await sut.GetAsync(It.IsAny <int>());

            // 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.GetByIdAsync(It.IsAny <int>()));
        }
コード例 #2
0
        public async void GetErrorTest()
        {
            // 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.GetByIdAsync(It.IsAny <int>()))
            .Throws(new Exception());
            mockContact.Setup(c => c.Id).Returns(It.IsAny <int>());

            // Act
            var sut = new ContactApplicationService(
                mockLogManager.Object, mockContactRepository.Object, mockContactValidator.Object, mockAddressApplicationService.Object);
            var response = await sut.GetAsync(It.IsAny <int>());

            // 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.
            mockContactRepository.Verify(x => x.GetByIdAsync(It.IsAny <int>()));

            // Verify the application service is logging the error.
            mockLogManager.Verify(x => x.LogError(It.IsAny <Exception>(), It.IsAny <string>()));
        }