public void ShouldThrowServiceExceptionOnRetrieveAllRegistrationsWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedRegistrationServiceException = new RegistrationServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllRegistrations()) .Throws(exception); // when . then Assert.Throws <RegistrationServiceException>(() => this.registrationService.RetrieveAllRegistrations()); this.storageBrokerMock.Verify(broker => broker.SelectAllRegistrations(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedRegistrationServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync() { // given Guid someRegistrationId = Guid.NewGuid(); var exception = new Exception(); var expectedRegistrationServiceException = new RegistrationServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(exception); // when ValueTask <Registration> retrieveByIdRegistrationTask = this.registrationService.RetrieveRegistrationByIdAsync(someRegistrationId); // then await Assert.ThrowsAsync <RegistrationServiceException>(() => retrieveByIdRegistrationTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedRegistrationServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private RegistrationServiceException CreateAndLogServiceException(Exception exception) { var registrationServiceException = new RegistrationServiceException(exception); this.loggingBroker.LogError(registrationServiceException); return(registrationServiceException); }
public async Task ShouldThrowServiceExceptionOnModifyIfExceptionOccursAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); Registration someRegistration = CreateRandomRegistration(dateTime: randomDateTime); var serviceException = new Exception(); var failedRegistrationServiceException = new FailedRegistrationServiceException(serviceException); var expectedRegistrationServiceException = new RegistrationServiceException(failedRegistrationServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(serviceException); // when ValueTask <Registration> modifyRegistrationTask = this.registrationService.ModifyRegistrationAsync(someRegistration); // then await Assert.ThrowsAsync <RegistrationServiceException>(() => modifyRegistrationTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedRegistrationServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateRegistrationAsync(It.IsAny <Registration>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Registration randomRegistration = CreateRandomRegistration(dateTime); Registration inputRegistration = randomRegistration; inputRegistration.UpdatedBy = inputRegistration.CreatedBy; var serviceException = new Exception(); var failedRegistrationServiceException = new FailedRegistrationServiceException(serviceException); var expectedRegistrationServiceException = new RegistrationServiceException(failedRegistrationServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertRegistrationAsync(inputRegistration)) .ThrowsAsync(serviceException); // when ValueTask <Registration> createRegistrationTask = this.registrationService.AddRegistrationAsync(inputRegistration); // then await Assert.ThrowsAsync <RegistrationServiceException>(() => createRegistrationTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedRegistrationServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertRegistrationAsync(inputRegistration), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRemoveIfExceptionOccursAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); Registration someRegistration = CreateRandomRegistration(dateTime: randomDateTime); Guid someRegistrationId = someRegistration.Id; Registration storageRegistration = someRegistration; var exception = new Exception(); var expectedServiceException = new RegistrationServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(storageRegistration); this.storageBrokerMock.Setup(broker => broker.DeleteRegistrationAsync(It.IsAny <Registration>())) .ThrowsAsync(exception); // when ValueTask <Registration> deleteRegistrationTask = this.registrationService.RemoveRegistrationByIdAsync(someRegistrationId); // then await Assert.ThrowsAsync <RegistrationServiceException>(() => deleteRegistrationTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteRegistrationAsync(It.IsAny <Registration>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }