public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            UserContact randomUserContact = CreateRandomUserContact();
            UserContact inputUserContact  = randomUserContact;
            var         exception         = new Exception();

            var expectedUserContactServiceException =
                new UserContactServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertUserContactAsync(inputUserContact))
            .ThrowsAsync(exception);

            // when
            ValueTask <UserContact> addUserContactTask =
                this.userContactService.AddUserContactAsync(inputUserContact);

            // then
            await Assert.ThrowsAsync <UserContactServiceException>(() =>
                                                                   addUserContactTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedUserContactServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertUserContactAsync(inputUserContact),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedUserContactServiceException =
                new UserContactServiceException(exception);

            this.storageBrokerMock.Setup(broker => broker.SelectAllUserContacts())
            .Throws(exception);

            // when . then
            Assert.Throws <UserContactServiceException>(() =>
                                                        this.userContactService.RetrieveAllUserContacts());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedUserContactServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker => broker.SelectAllUserContacts(),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private UserContactServiceException CreateAndLogServiceException(Exception exception)
        {
            var UserContactServiceException = new UserContactServiceException(exception);

            this.loggingBroker.LogError(UserContactServiceException);

            return(UserContactServiceException);
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someContactId    = Guid.NewGuid();
            Guid someUserId       = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedUserContactServiceException =
                new FailedUserContactServiceException(serviceException);

            var expectedUserContactException =
                new UserContactServiceException(failedUserContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <UserContact> removeUserContactTask =
                this.userContactService.RemoveUserContactByIdAsync(
                    someUserId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <UserContactServiceException>(() =>
                                                                   removeUserContactTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedUserContactException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectUserContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteUserContactAsync(It.IsAny <UserContact>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            var  randomContactId = Guid.NewGuid();
            var  randomUserId    = Guid.NewGuid();
            Guid inputContactId  = randomContactId;
            Guid inputUserId     = randomUserId;
            var  exception       = new Exception();

            var expectedUserContactException =
                new UserContactServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(inputUserId, inputContactId))
            .ThrowsAsync(exception);

            // when
            ValueTask <UserContact> retrieveUserContactTask =
                this.userContactService.RetrieveUserContactByIdAsync(
                    inputUserId,
                    inputContactId);

            // then
            await Assert.ThrowsAsync <UserContactServiceException>(() =>
                                                                   retrieveUserContactTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedUserContactException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectUserContactByIdAsync(inputUserId, inputContactId),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }