예제 #1
0
        [InlineData(18, "", "TestEdit", 5, "Test", "Test", "Test", false)]       // Unsuccessful Edit Test #2
        public void TestSaveGroup(int groupId, string title, string desc, int maxUsers, string city, string country, string continent, bool expectingSuccess)
        {
            var controllerContextMock = new Mock <ControllerContext>()
            {
                CallBase = true
            };
            var contextMock = new Mock <ApplicationDbContext>()
            {
                CallBase = true
            };

            contextMock.Setup(c => c.SaveChanges()).Returns(1);

            var controller = new GroupsController
            {
                ControllerContext = controllerContextMock.Object,
                Context           = contextMock.Object,
                GetUserId         = () => "52a0d912-33dd-42fc-9b5b-959ed573e6dd"
            };

            var viewModel = new CreateGroupViewModel()
            {
                City                = city,
                Country             = country,
                Continent           = continent,
                GroupId             = groupId,
                DetailedDescription = desc,
                MaxUserCapacity     = maxUsers,
                Title               = title
            };

            if (expectingSuccess)
            {
                var result = (RedirectToRouteResult)controller.Save(viewModel);
                Assert.Equal("UserGroups", result.RouteValues["action"]);
            }
            else
            {
                // check if expected fail is because of model state.
                var valContext = new ValidationContext(viewModel, null, null);
                var valResults = new List <ValidationResult>();
                if (Validator.TryValidateObject(viewModel, valContext, valResults, true))
                {
                    var result = controller.Save(viewModel);
                    Assert.True(result is HttpNotFoundResult);
                }
                else
                {
                    // This means that the passed model was not valid, meaning the system will not successfully save.
                    // However, ModelState object of the controller does not work properly in unit test cases.
                    // So, we deem this test a success, as if it had failed.
                    Assert.True(true);
                }
            }
        }