コード例 #1
0
        public void ShouldThrowServiceExceptionOnRetrieveAllStudentAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(exception);

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

            // when . then
            Assert.Throws <StudentAttachmentServiceException>(() =>
                                                              this.studentAttachmentService.RetrieveAllStudentAttachments());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment();
            StudentAttachment inputStudentAttachment  = randomStudentAttachment;
            var exception = new Exception();

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(inputStudentAttachment))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(inputStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         addStudentAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #3
0
        private StudentAttachmentServiceException CreateAndLogServiceException(Exception exception)
        {
            var StudentAttachmentServiceException = new StudentAttachmentServiceException(exception);

            this.loggingBroker.LogError(StudentAttachmentServiceException);

            return(StudentAttachmentServiceException);
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            var  randomAttachmentId = Guid.NewGuid();
            var  randomStudentId    = Guid.NewGuid();
            Guid someAttachmentId   = randomAttachmentId;
            Guid someStudentId      = randomStudentId;
            var  serviceException   = new Exception();

            var failedStudentAttachmentServiceException =
                new FailedStudentAttachmentServiceException(serviceException);

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(failedStudentAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentAttachment> removeStudentAttachmentTask =
                this.studentAttachmentService.RemoveStudentAttachmentByIdAsync(
                    someStudentId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         removeStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomAttachmentId = Guid.NewGuid();
            Guid randomStudentId    = Guid.NewGuid();
            Guid inputAttachmentId  = randomAttachmentId;
            Guid inputStudentId     = randomStudentId;
            var  exception          = new Exception();

            var expectedStudentAttachmentException =
                new StudentAttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentAttachment> retrieveStudentAttachmentTask =
                this.studentAttachmentService.RetrieveStudentAttachmentByIdAsync
                    (inputStudentId, inputAttachmentId);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         retrieveStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentAttachmentByIdAsync(inputStudentId, inputAttachmentId),
                                          Times.Once);

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