public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedTeacherContactServiceException =
                new FailedTeacherContactServiceException(serviceException);

            var expectedTeacherContactServiceException =
                new TeacherContactServiceException(failedTeacherContactServiceException);

            this.storageBrokerMock.Setup(broker => broker.SelectAllTeacherContacts())
            .Throws(serviceException);

            // when
            Action retrieveAllTeacherContactsAction = () =>
                                                      this.teacherContactService.RetrieveAllTeacherContacts();

            // then
            Assert.Throws <TeacherContactServiceException>(
                retrieveAllTeacherContactsAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            TeacherContact randomTeacherContact = CreateRandomTeacherContact();
            TeacherContact inputTeacherContact  = randomTeacherContact;
            var            exception            = new Exception();

            var expectedTeacherContactServiceException =
                new TeacherContactServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherContactAsync(inputTeacherContact))
            .ThrowsAsync(exception);

            // when
            ValueTask <TeacherContact> addTeacherContactTask =
                this.teacherContactService.AddTeacherContactAsync(inputTeacherContact);

            // then
            await Assert.ThrowsAsync <TeacherContactServiceException>(() =>
                                                                      addTeacherContactTask.AsTask());

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

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

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

            this.loggingBroker.LogError(teacherContactServiceException);

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

            var failedTeacherContactServiceException =
                new FailedTeacherContactServiceException(serviceException);

            var expectedTeacherContactException =
                new TeacherContactServiceException(
                    failedTeacherContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <TeacherContact> removeTeacherContactTask =
                this.teacherContactService.RemoveTeacherContactByIdAsync(
                    someTeacherId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <TeacherContactServiceException>(() =>
                                                                      removeTeacherContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId),
                                          Times.Once);

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

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