public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; inputGuardian.UpdatedBy = inputGuardian.CreatedBy; var exception = new Exception(); var expectedGuardianServiceException = new GuardianServiceException(exception); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAsync(inputGuardian)) .ThrowsAsync(exception); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianServiceException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(inputGuardian), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomGuardianId = Guid.NewGuid(); Guid inputGuardianId = randomGuardianId; var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(inputGuardianId)) .ThrowsAsync(serviceException); // when ValueTask <Guardian> deleteGuardianTask = this.guardianService.RemoveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianServiceException>(() => deleteGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(inputGuardianId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllGuardians()) .Throws(serviceException); // when Action retrieveAllGuardiansAction = () => this.guardianService.RetrieveAllGuardians(); // then Assert.Throws <GuardianServiceException>( retrieveAllGuardiansAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllGuardians(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }