public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync() { // given StudentContact randomStudentContact = CreateRandomStudentContact(); StudentContact inputStudentContact = randomStudentContact; var databaseUpdateException = new DbUpdateException(); var expectedStudentContactDependencyException = new StudentContactDependencyException(databaseUpdateException); this.storageBrokerMock.Setup(broker => broker.InsertStudentContactAsync(inputStudentContact)) .ThrowsAsync(databaseUpdateException); // when ValueTask <StudentContact> addStudentContactTask = this.studentContactService.AddStudentContactAsync(inputStudentContact); // then await Assert.ThrowsAsync <StudentContactDependencyException>(() => addStudentContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentContactDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentContactAsync(inputStudentContact), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() { // given SqlException sqlException = GetSqlException(); var expectedStudentContactDependencyException = new StudentContactDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAllStudentContacts()) .Throws(sqlException); //when. then Assert.Throws<StudentContactDependencyException>(() => this.studentContactService.RetrieveAllStudentContacts()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedStudentContactDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllStudentContacts(), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogItAsync() { // given Guid someContactId = Guid.NewGuid(); Guid someStudentId = Guid.NewGuid(); var databaseUpdateException = new DbUpdateException(); var expectedStudentContactDependencyException = new StudentContactDependencyException(databaseUpdateException); this.storageBrokerMock.Setup(broker => broker.SelectStudentContactByIdAsync(someStudentId, someContactId)) .ThrowsAsync(databaseUpdateException); // when ValueTask <StudentContact> retrieveStudentContactTask = this.studentContactService.RetrieveStudentContactByIdAsync (someStudentId, someContactId); // then await Assert.ThrowsAsync <StudentContactDependencyException>( () => retrieveStudentContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentContactDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentContactByIdAsync(someStudentId, someContactId), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private StudentContactDependencyException CreateAndLogDependencyException(Exception exception) { var StudentContactDependencyException = new StudentContactDependencyException(exception); this.loggingBroker.LogError(StudentContactDependencyException); return(StudentContactDependencyException); }
private StudentContactDependencyException CreateAndLogCriticalDependencyException(Exception exception) { var studentContactDependencyException = new StudentContactDependencyException(exception); this.loggingBroker.LogCritical(studentContactDependencyException); return(studentContactDependencyException); }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync() { // given var randomContactId = Guid.NewGuid(); var randomStudentId = Guid.NewGuid(); Guid someContactId = randomContactId; Guid someStudentId = randomStudentId; SqlException sqlException = GetSqlException(); var expectedStudentContactDependencyException = new StudentContactDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectStudentContactByIdAsync(someStudentId, someContactId)) .ThrowsAsync(sqlException); // when ValueTask <StudentContact> removeStudentContactTask = this.studentContactService.RemoveStudentContactByIdAsync( someStudentId, someContactId); // then await Assert.ThrowsAsync <StudentContactDependencyException>(() => removeStudentContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs( expectedStudentContactDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentContactByIdAsync(someStudentId, someContactId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteStudentContactAsync(It.IsAny <StudentContact>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }