public async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() { // given Guid someTeacherId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedTeacherException = new LockedTeacherException(databaseUpdateConcurrencyException); var expectedTeacherDependencyValidationException = new TeacherDependencyValidationException(lockedTeacherException); this.storageBrokerMock.Setup(broker => broker.SelectTeacherByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Teacher> deleteTeacherTask = this.teacherService.RemoveTeacherByIdAsync(someTeacherId); // then await Assert.ThrowsAsync <TeacherDependencyValidationException>(() => deleteTeacherTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherDependencyValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherByIdAsync(It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private Exception CreateAndLogDependencyValidationException(Xeption exception) { var teacherDependencyValidationException = new TeacherDependencyValidationException(exception); this.loggingBroker.LogError(teacherDependencyValidationException); return(teacherDependencyValidationException); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Teacher someTeacher = CreateRandomTeacher(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedTeacherException = new LockedTeacherException(databaseUpdateConcurrencyException); var expectedTeacherDependencyValidationException = new TeacherDependencyValidationException(lockedTeacherException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Throws(databaseUpdateConcurrencyException); // when ValueTask <Teacher> modifyTeacherTask = this.teacherService.ModifyTeacherAsync(someTeacher); // then await Assert.ThrowsAsync <TeacherDependencyValidationException>(() => modifyTeacherTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherByIdAsync(It.IsAny <Guid>()), Times.Never); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherDependencyValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateTeacherAsync(It.IsAny <Teacher>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyValidationExceptionOnAddIfTeacherAlreadyExsitsAndLogItAsync() { // given var someTeacher = CreateRandomTeacher(); var alreadyExistsTeacher = someTeacher; string randomMessage = GetRandomMessage(); var duplicateKeyException = new DuplicateKeyException(randomMessage); var alreadyExistsTeacherException = new AlreadyExistsTeacherException(duplicateKeyException); var expectedTeacherDependencyValidationException = new TeacherDependencyValidationException(alreadyExistsTeacherException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Throws(duplicateKeyException); // when ValueTask <Teacher> addTeacherTask = this.teacherService.CreateTeacherAsync(alreadyExistsTeacher); // then await Assert.ThrowsAsync <TeacherDependencyValidationException>(() => addTeacherTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherDependencyValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAsync(It.IsAny <Teacher>()), Times.Never()); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }