Пример #1
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            GuardianContact randomGuardianContact = CreateRandomGuardianContact();
            GuardianContact inputGuardianContact  = randomGuardianContact;
            var             exception             = new Exception();

            var expectedGuardianContactServiceException =
                new GuardianContactServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianContactAsync(inputGuardianContact))
            .ThrowsAsync(exception);

            // when
            ValueTask <GuardianContact> addGuardianContactTask =
                this.guardianContactService.AddGuardianContactAsync(inputGuardianContact);

            // then
            await Assert.ThrowsAsync <GuardianContactServiceException>(() =>
                                                                       addGuardianContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedGuardianContactServiceException =
                new GuardianContactServiceException(exception);

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

            // when . then
            Assert.Throws <GuardianContactServiceException>(() =>
                                                            this.guardianContactService.RetrieveAllGuardianContacts());

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

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

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

            this.loggingBroker.LogError(guardianContactServiceException);

            return(guardianContactServiceException);
        }
Пример #4
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            var  randomContactId  = Guid.NewGuid();
            var  randomGuardianId = Guid.NewGuid();
            Guid someContactId    = randomContactId;
            Guid someGuardianId   = randomGuardianId;
            var  serviceException = new Exception();

            var failedGuardianContactServiceException =
                new FailedGuardianContactServiceException(serviceException);

            var expectedGuardianContactServiceException =
                new GuardianContactServiceException(failedGuardianContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <GuardianContact> removeGuardianContactTask =
                this.guardianContactService.RemoveGuardianContactByIdAsync(
                    someGuardianId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <GuardianContactServiceException>(() =>
                                                                       removeGuardianContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId),
                                          Times.Once);

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

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