public void ShouldThrowServiceExceptionOnRetrieveAllGuardianAttachmentsWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedGuardianAttachmentServiceException = new GuardianAttachmentServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllGuardianAttachments()) .Throws(exception); // when . then Assert.Throws <GuardianAttachmentServiceException>(() => this.guardianAttachmentService.RetrieveAllGuardianAttachments()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllGuardianAttachments(), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment(); GuardianAttachment someGuardianAttachment = randomGuardianAttachment; var exception = new Exception(); var expectedGuardianAttachmentServiceException = new GuardianAttachmentServiceException(exception); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAttachmentAsync(someGuardianAttachment)) .ThrowsAsync(exception); // when ValueTask <GuardianAttachment> addGuardianAttachmentTask = this.guardianAttachmentService.AddGuardianAttachmentAsync(someGuardianAttachment); // then await Assert.ThrowsAsync <GuardianAttachmentServiceException>(() => addGuardianAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAttachmentAsync(someGuardianAttachment), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private GuardianAttachmentServiceException CreateAndLogServiceException(Exception exception) { var guardianAttachmentServiceException = new GuardianAttachmentServiceException(exception); this.loggingBroker.LogError(guardianAttachmentServiceException); return(guardianAttachmentServiceException); }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given var randomAttachmentId = Guid.NewGuid(); var randomGuardianId = Guid.NewGuid(); Guid someAttachmentId = randomAttachmentId; Guid someGuardianId = randomGuardianId; var serviceException = new Exception(); var failedGuardianAttachmentServiceException = new FailedGuardianAttachmentServiceException(serviceException); var expectedGuardianAttachmentException = new GuardianAttachmentServiceException(failedGuardianAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId)) .ThrowsAsync(serviceException); // when ValueTask <GuardianAttachment> removeGuardianAttachmentTask = this.guardianAttachmentService.RemoveGuardianAttachmentByIdAsync( someGuardianId, someAttachmentId); // then await Assert.ThrowsAsync <GuardianAttachmentServiceException>(() => removeGuardianAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAttachmentAsync(It.IsAny <GuardianAttachment>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }