Beispiel #1
0
        public async Task EditPost_WhenIdEqualsDivisionIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndDivisionWithIdExists_ShouldRethrowException()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();

            var divisionRepository = A.Fake <IDivisionRepository>();

            A.CallTo(() => divisionRepository.Update(A <Division> .Ignored)).Throws <DbUpdateConcurrencyException>();
            A.CallTo(() => divisionRepository.DivisionExists(A <int> .Ignored)).Returns(true);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                           divisionRepository, sharedRepository);

            int id       = 1;
            var division = new Division
            {
                ID = 1
            };

            // Act
            var func = new Func <Task <IActionResult> >(async() => await testController.Edit(id, division));

            // Assert
            await func.ShouldThrowAsync <DbUpdateConcurrencyException>();
        }
Beispiel #2
0
        public async Task EditPost_WhenIdEqualsDivisionIdAndModelStateIsNotValid_ShouldReturnDivisionEditView()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();
            var divisionRepository        = A.Fake <IDivisionRepository>();
            var sharedRepository          = A.Fake <ISharedRepository>();
            var testController            = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                                    divisionRepository, sharedRepository);

            int id       = 1;
            var division = new Division
            {
                ID = 1
            };

            testController.ModelState.AddModelError("LongName", "Please enter a long name.");

            // Act
            var result = await testController.Edit(id, division);

            // Assert
            A.CallTo(() => divisionRepository.Update(A <Division> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustNotHaveHappened();
            result.ShouldBeOfType <ViewResult>();
            ((ViewResult)result).Model.ShouldBe(division);
        }
Beispiel #3
0
        public async Task EditPost_WhenIdEqualsDivisionIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndDivisionWithIdDoesNotExist_ShouldReturnNotFound()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();

            var divisionRepository = A.Fake <IDivisionRepository>();

            A.CallTo(() => divisionRepository.Update(A <Division> .Ignored)).Throws <DbUpdateConcurrencyException>();
            A.CallTo(() => divisionRepository.DivisionExists(A <int> .Ignored)).Returns(false);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                           divisionRepository, sharedRepository);

            int id       = 1;
            var division = new Division
            {
                ID = 1
            };

            // Act
            var result = await testController.Edit(id, division);

            // Assert
            result.ShouldBeOfType <NotFoundResult>();
        }
Beispiel #4
0
        public async Task EditPost_WhenIdEqualsDivisionIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsNotCaught_ShouldUpdateDivisionInDataStoreAndRedirectToIndexView()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();
            var divisionRepository        = A.Fake <IDivisionRepository>();
            var sharedRepository          = A.Fake <ISharedRepository>();
            var testController            = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                                    divisionRepository, sharedRepository);

            int id       = 1;
            var division = new Division
            {
                ID = 1
            };

            // Act
            var result = await testController.Edit(id, division);

            // Assert
            A.CallTo(() => divisionRepository.Update(division)).MustHaveHappenedOnceExactly();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly();
            result.ShouldBeOfType <RedirectToActionResult>();
            ((RedirectToActionResult)result).ActionName.ShouldBe <string>(nameof(testController.Index));
        }
Beispiel #5
0
        public async Task EditGet_WhenIdIsNotNullAndDivisionFound_ShouldReturnDivisionEditView()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();

            var      divisionRepository = A.Fake <IDivisionRepository>();
            Division?division           = new Division();

            A.CallTo(() => divisionRepository.GetDivisionAsync(A <int> .Ignored)).Returns(division);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                           divisionRepository, sharedRepository);

            int?id = 0;

            // Act
            var result = await testController.Edit(id);

            // Assert
            A.CallTo(() => divisionRepository.GetDivisionAsync(id.Value)).MustHaveHappenedOnceExactly();
            result.ShouldBeOfType <ViewResult>();
            ((ViewResult)result).Model.ShouldBe(division);
        }
Beispiel #6
0
        public async Task EditGet_WhenIdIsNull_ShouldReturnNotFound()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();
            var divisionRepository        = A.Fake <IDivisionRepository>();
            var sharedRepository          = A.Fake <ISharedRepository>();
            var testController            = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                                    divisionRepository, sharedRepository);

            int?id = null;

            // Act
            var result = await testController.Edit(id);

            // Assert
            result.ShouldBeOfType <NotFoundResult>();
        }
Beispiel #7
0
        public async Task EditPost_WhenIdDoesNotEqualDivisionId_ShouldReturnNotFound()
        {
            // Arrange
            var divisionsIndexViewModel   = A.Fake <IDivisionsIndexViewModel>();
            var divisionsDetailsViewModel = A.Fake <IDivisionsDetailsViewModel>();
            var divisionRepository        = A.Fake <IDivisionRepository>();
            var sharedRepository          = A.Fake <ISharedRepository>();
            var testController            = new DivisionsController(divisionsIndexViewModel, divisionsDetailsViewModel,
                                                                    divisionRepository, sharedRepository);

            int id       = 0;
            var division = new Division
            {
                ID = 1
            };

            // Act
            var result = await testController.Edit(id, division);

            // Assert
            result.ShouldBeOfType <NotFoundResult>();
        }