public async Task ShouldThrowValidationExceptionOnModifyWhenCategoryTitleIsInvalidAndLogItAsync( string invalidCategoryName) { // given DateTimeOffset dateTime = GetRandomDateTime(); Category randomCategory = CreateRandomCategory(dateTime); Category invalidCategory = randomCategory; invalidCategory.Title = invalidCategoryName; var invalidCategoryException = new InvalidCategoryException( parameterName: nameof(Category.Title), parameterValue: invalidCategory.Title); var expectedCategoryValidationException = new CategoryValidationException(invalidCategoryException); // when ValueTask <Category> modifyCategoryTask = this.categoryService.ModifyCategoryAsync(invalidCategory); // then await Assert.ThrowsAsync <CategoryValidationException>(() => modifyCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(int minutes) { // given DateTimeOffset dateTime = GetRandomDateTime(); Category randomCategory = CreateRandomCategory(dateTime); Category inputCategory = randomCategory; inputCategory.UpdatedBy = randomCategory.CreatedBy; inputCategory.CreatedDate = dateTime.AddMinutes(minutes); inputCategory.UpdatedDate = inputCategory.CreatedDate; var invalidCategoryValidationException = new InvalidCategoryException( parameterName: nameof(Category.CreatedDate), parameterValue: inputCategory.CreatedDate); var expectedCategoryValidationException = new CategoryValidationException(invalidCategoryValidationException); // when ValueTask <Category> createCategoryTask = this.categoryService.AddCategoryAsync(inputCategory); // then await Assert.ThrowsAsync <CategoryValidationException>(() => createCategoryTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCategoryAsync(It.IsAny <Category>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnDeleteWhenIdIsInvalidAndLogItAsync() { // given Guid randomCategoryId = default; Guid inputCategoryId = randomCategoryId; var invalidCategoryInputException = new InvalidCategoryException( parameterName: nameof(Category.Id), parameterValue: inputCategoryId); var expectedCategoryValidationException = new CategoryValidationException(invalidCategoryInputException); // when ValueTask <Category> deleteCategoryTask = this.categoryService.RemoveCategoryByIdAsync(inputCategoryId); // then await Assert.ThrowsAsync <CategoryValidationException>(() => deleteCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCategoryByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteCategoryAsync(It.IsAny <Category>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenIdIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Category randomCategory = CreateRandomCategory(dateTime); Category inputCategory = randomCategory; inputCategory.Id = default; var invalidCategoryInputException = new InvalidCategoryException( parameterName: nameof(Category.Id), parameterValue: inputCategory.Id); var expectedCategoryValidationException = new CategoryValidationException(invalidCategoryInputException); // when ValueTask <Category> modifyCategoryTask = this.categoryService.ModifyCategoryAsync(inputCategory); // then await Assert.ThrowsAsync <CategoryValidationException>(() => modifyCategoryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCategoryValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCategoryByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }