コード例 #1
0
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomAttendanceId = Guid.NewGuid();
            Guid inputAttendanceId  = randomAttendanceId;
            var  exception          = new Exception();

            var expectedAttendanceServiceException =
                new AttendanceServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(inputAttendanceId))
            .ThrowsAsync(exception);

            // when
            ValueTask <Attendance> deleteAttendanceTask =
                this.attendanceService.RemoveAttendanceByIdAsync(inputAttendanceId);

            // then
            await Assert.ThrowsAsync <AttendanceServiceException>(() =>
                                                                  deleteAttendanceTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedAttendanceServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(inputAttendanceId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedAttendanceServiceException =
                new AttendanceServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllAttendances())
            .Throws(exception);

            // when . then
            Assert.Throws <AttendanceServiceException>(() =>
                                                       this.attendanceService.RetrieveAllAttendances());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedAttendanceServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAllAttendances(),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #3
0
        private AttendanceServiceException CreateAndLogServiceException(Exception exception)
        {
            var attendanceServiceException = new AttendanceServiceException(exception);

            this.loggingBroker.LogError(attendanceServiceException);

            return(attendanceServiceException);
        }
コード例 #4
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Attendance     randomAttendance     = CreateRandomAttendance(randomDateTime);
            Attendance     someAttendance       = randomAttendance;

            someAttendance.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedAttendanceServiceException =
                new FailedAttendanceServiceException(serviceException);

            var expectedAttendanceServiceException =
                new AttendanceServiceException(failedAttendanceServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(someAttendance.Id))
            .ThrowsAsync(serviceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDateTime);

            // when
            ValueTask <Attendance> modifyAttendanceTask =
                this.attendanceService.ModifyAttendanceAsync(someAttendance);

            // then
            await Assert.ThrowsAsync <AttendanceServiceException>(() =>
                                                                  modifyAttendanceTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(someAttendance.Id),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedAttendanceServiceException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
コード例 #5
0
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime         = GetRandomDateTime();
            Attendance     randomAttendance = CreateRandomAttendance(dateTime);
            Attendance     inputAttendance  = randomAttendance;

            inputAttendance.UpdatedBy = inputAttendance.CreatedBy;
            var serviceException = new Exception();

            var failedAttendanceServiceException =
                new FailedAttendanceServiceException(serviceException);

            var expectedAttendanceServiceException =
                new AttendanceServiceException(failedAttendanceServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAttendanceAsync(inputAttendance))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Attendance> createAttendanceTask =
                this.attendanceService.CreateAttendanceAsync(inputAttendance);

            // then
            await Assert.ThrowsAsync <AttendanceServiceException>(() =>
                                                                  createAttendanceTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedAttendanceServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertAttendanceAsync(inputAttendance),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.AtLeastOnce);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #6
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttendanceId = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedAttendanceServiceException =
                new FailedAttendanceServiceException(serviceException);

            var expectedAttendanceServiceException =
                new AttendanceServiceException(failedAttendanceServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttendanceByIdAsync(someAttendanceId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Attendance> retrieveAttendanceByIdTask =
                this.attendanceService.RetrieveAttendanceByIdAsync(someAttendanceId);

            // then
            await Assert.ThrowsAsync <AttendanceServiceException>(() =>
                                                                  retrieveAttendanceByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedAttendanceServiceException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttendanceByIdAsync(someAttendanceId),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }