public void ShouldThrowServiceExceptionOnRetrieveAllExamAttachmentsWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedExamAttachmentServiceException = new ExamAttachmentServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllExamAttachments()) .Throws(exception); // when . then Assert.Throws <ExamAttachmentServiceException>(() => this.examAttachmentService.RetrieveAllExamAttachments()); this.storageBrokerMock.Verify(broker => broker.SelectAllExamAttachments(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamAttachmentServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given ExamAttachment randomExamAttachment = CreateRandomExamAttachment(); ExamAttachment someExamAttachment = randomExamAttachment; var exception = new Exception(); var expectedExamAttachmentServiceException = new ExamAttachmentServiceException(exception); this.storageBrokerMock.Setup(broker => broker.InsertExamAttachmentAsync(someExamAttachment)) .ThrowsAsync(exception); // when ValueTask <ExamAttachment> addExamAttachmentTask = this.examAttachmentService.AddExamAttachmentAsync(someExamAttachment); // then await Assert.ThrowsAsync <ExamAttachmentServiceException>(() => addExamAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedExamAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertExamAttachmentAsync(someExamAttachment), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private ExamAttachmentServiceException CreateAndLogServiceException(Exception exception) { var ExamAttachmentServiceException = new ExamAttachmentServiceException(exception); this.loggingBroker.LogError(ExamAttachmentServiceException); return(ExamAttachmentServiceException); }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someExamId = Guid.NewGuid(); var serviceException = new Exception(); var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(serviceException); var expectedExamAttachmentException = new ExamAttachmentServiceException(failedExamAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectExamAttachmentByIdAsync(someExamId, someAttachmentId)) .ThrowsAsync(serviceException); // when ValueTask <ExamAttachment> removeExamAttachmentTask = this.examAttachmentService.RemoveExamAttachmentByIdAsync( someExamId, someAttachmentId); // then await Assert.ThrowsAsync <ExamAttachmentServiceException>(() => removeExamAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectExamAttachmentByIdAsync(someExamId, someAttachmentId), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteExamAttachmentAsync(It.IsAny <ExamAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }