public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomCategoryId = Guid.NewGuid(); Guid inputCategoryId = randomCategoryId; var exception = new Exception(); var expectedStudentCategoryException = new CategoryServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectCategoryByIdAsync(inputCategoryId)) .ThrowsAsync(exception); // when ValueTask <Category> deleteStudentCategoryTask = this.categoryService.RemoveCategoryByIdAsync(inputCategoryId); // then await Assert.ThrowsAsync <CategoryServiceException>(() => deleteStudentCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentCategoryException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCategoryByIdAsync(inputCategoryId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllCategoriesWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedCategoryServiceException = new CategoryServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectAllCategories()) .Throws(exception); // when . then Assert.Throws <CategoryServiceException>(() => this.categoryService.RetrieveAllCategories()); this.storageBrokerMock.Verify(broker => broker.SelectAllCategories(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync() { // given Guid someCategoryId = Guid.NewGuid(); var exception = new Exception(); var expectedCategoryServiceException = new CategoryServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectCategoryByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(exception); // when ValueTask <Category> retrieveByIdCategoryTask = this.categoryService.RetrieveCategoryByIdAsync(someCategoryId); // then await Assert.ThrowsAsync <CategoryServiceException>(() => retrieveByIdCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCategoryByIdAsync(It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private CategoryServiceException CreateAndLogServiceException(Exception exception) { var categoryServiceException = new CategoryServiceException(exception); this.loggingBroker.LogError(categoryServiceException); return(categoryServiceException); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Category randomCategory = CreateRandomCategory(dateTime); Category inputCategory = randomCategory; inputCategory.UpdatedBy = inputCategory.CreatedBy; var exception = new Exception(); var expectedCategoryServiceException = new CategoryServiceException(exception); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCategoryAsync(inputCategory)) .ThrowsAsync(exception); // when ValueTask <Category> createCategoryTask = this.categoryService.AddCategoryAsync(inputCategory); // then await Assert.ThrowsAsync <CategoryServiceException>(() => createCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCategoryAsync(inputCategory), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Category randomCategory = CreateRandomCategory(randomDateTime); Category someCategory = randomCategory; someCategory.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var expectedCategoryServiceException = new CategoryServiceException(serviceException); this.storageBrokerMock.Setup(broker => broker.SelectCategoryByIdAsync(someCategory.Id)) .ThrowsAsync(serviceException); // when ValueTask <Category> modifyCategoryTask = this.categoryService.ModifyCategoryAsync(someCategory); // then await Assert.ThrowsAsync <CategoryServiceException>(() => modifyCategoryTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCategoryByIdAsync(someCategory.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }