コード例 #1
0
        public void DeleteTimeReport_ThrowsInternalServerErrorException(RegistriesDelete registries)
        {
            //Arrange
            Registry reg1 = new Registry
            {
                RegistryId = 1,
                TaskId     = 1,
                UserId     = 1,
                Hours      = 2,
                Created    = new DateTime(2021 - 01 - 04),
                Date       = new DateTime(2021 - 01 - 04),
                Invoice    = InvoiceType.Invoicable
            };
            List <Registry> mockedRegistries = new List <Registry> {
                reg1
            };

            Mock <IRegistryRepository> registryRepoMock = new Mock <IRegistryRepository>();

            registryRepoMock.Setup(r => r.GetById(1)).Returns(reg1);
            registryRepoMock.Setup(r => r.Delete(It.IsAny <object>())).Throws <Exception>();

            Mock <IUnitOfWork> mockUOF = new Mock <IUnitOfWork>();

            mockUOF.Setup(uow => uow.RegistryRepository).Returns(registryRepoMock.Object);

            var controller = new ReportingController(mockUOF.Object, httpContextAccessorMock);

            //Act
            var result = controller.DeleteTimeReport(registries);

            //Assert
            Assert.IsType <ActionResult <HttpResponse> >(result);
            Assert.Equal((int)HttpStatusCode.InternalServerError, (result.Result as ObjectResult).StatusCode);
        }
コード例 #2
0
        public void DeleteTimeReport_SuccessTest(RegistriesDelete registries)
        {
            //Arrange
            List <Registry>            weekRegistries   = GetTestRegistries();
            Mock <IRegistryRepository> registryRepoMock = new Mock <IRegistryRepository>();

            registryRepoMock.Setup(r => r.GetById(1)).Returns(weekRegistries[0]);
            registryRepoMock.Setup(r => r.GetById(2)).Returns(weekRegistries[1]);
            registryRepoMock.Setup(r => r.GetById(3)).Returns(weekRegistries[2]);

            registryRepoMock.Setup(r => r.Delete(It.IsAny <object>())).Callback <object>(regId => weekRegistries.RemoveAll(reg => reg.RegistryId == (int)regId));
            registryRepoMock.Setup(r => r.Save());

            Mock <IUnitOfWork> mockUOF = new Mock <IUnitOfWork>();

            mockUOF.Setup(uow => uow.RegistryRepository).Returns(registryRepoMock.Object);

            var controller = new ReportingController(mockUOF.Object, httpContextAccessorMock);

            //Act
            var result = controller.DeleteTimeReport(registries);

            //Assert
            Assert.IsType <ActionResult <HttpResponse> >(result);
            Assert.Equal((int)HttpStatusCode.OK, (result.Result as StatusCodeResult).StatusCode);
            Assert.Empty(weekRegistries);
            registryRepoMock.Verify(r => r.Delete(It.IsAny <object>()), Times.Exactly(3));
            registryRepoMock.Verify(r => r.Save(), Times.Once);
        }