public void TestUserController_Constructor()
 {
     IUnitOfWork unitOfWork = new UnitOfWork();
     UserController target = new UserController(unitOfWork);
     Assert.IsNotNull(unitOfWork.AddressRepository, "Address repository can not be null.");
     Assert.IsNotNull(unitOfWork.UserRepository, "User repository can not be null.");
     Assert.IsNotNull(target, "Controller can not be null.");
 }
        public void TestCreate_ReturnCreateView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            // Act
            ViewResult actual = target.Create();

            // Assert
            Assert.AreEqual("Create", actual.ViewName);
        }
        public void TestIndex_WhenNoError_ReturnIndexView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);
            Nullable<bool> errorOccurred = null;

            // Act
            ViewResult actual = target.Index(errorOccurred);

            // Assert
            Assert.AreEqual("Index", actual.ViewName);
            Assert.AreEqual(null, actual.ViewBag.ErrorMessage);

            //TODO: check number of records returned by model
        }
        public void TestProblem_ReturnError()
        {
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            ViewResult expected = new ViewResult();
            expected.ViewName = "Error";

            ViewResult actual = target.Problem() as ViewResult;
            Assert.AreEqual(expected.ViewName, actual.ViewName);
        }
        public void TestEdit_WhenUserIsDeleted_AddModelErrorAndReturnEditView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);
            Nullable<bool> errorOccurred = null;

            // Act
            ViewResult actual = target.Edit(2, errorOccurred) as ViewResult;

            // Assert
            Assert.AreEqual("Edit", actual.ViewName);
            Assert.IsFalse(target.ModelState.IsValid);
        }
        public void TestIndex_WhenError_ReturnIndexViewAndErrorMessage()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);
            Nullable<bool> errorOccurred = true;

            // Act
            ViewResult actual = target.Index(errorOccurred);

            // Assert
            Assert.AreEqual("Index", actual.ViewName);
            Assert.AreEqual("Error occurred.", actual.ViewBag.ErrorMessage);
        }
        public void TestEdit_WhenNoError_ReturnEditView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);
            Nullable<bool> errorOccurred = null;

            // Act
            ViewResult actual = target.Edit(1, errorOccurred) as ViewResult;

            // Assert
            Assert.AreEqual("Edit", actual.ViewName);
        }
        public void TestEdit_WhenUserIDIsZero_ReturnIndexViewAndErrorMessage()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);
            Nullable<bool> errorOccurred = null;

            // Act
            RedirectToRouteResult actual = target.Edit(0, errorOccurred) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual("Index", actual.RouteValues["action"]);
            Assert.AreEqual(true, actual.RouteValues["errorOccurred"]);
        }
        public void TestEdit_WhenNoError_EditUserAndReturnIndexView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            var editedUser = userRepository.GetUserByID(1);
            editedUser.FirstName = "Edited_First_Name";
            editedUser.LastName = "Edited_Last_Name";
            editedUser.Email = "*****@*****.**";

            // Act
            RedirectToRouteResult actual = target.Edit(editedUser) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual("Index", actual.RouteValues["action"]);
            Assert.IsNotNull(editedUser.DateModified);
            Assert.IsNull(editedUser.IsDeleted);
        }
        public void TestEdit_WhenError_ModelStateNotValid_ReturnEditView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            var editedUser = userRepository.GetUserByID(1);
            editedUser.FirstName = "";
            editedUser.LastName = "";
            editedUser.Email = "";
            //TODO: it shows that modelstate is valid, throws an exception when saving

            // Act
            ViewResult actual = target.Edit(editedUser) as ViewResult;

            // Assert
            Assert.AreEqual("Edit", actual.ViewName);
            Assert.IsFalse(target.ModelState.IsValid);
        }
        public void TestCreate_WhenNoError_InsertUserAndReturnIndexView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            var newUser = userRepository.GetUserByID(1);

            User user = new User()
            {
                Email = "*****@*****.**",
                FirstName = "FirstName1",
                LastName = "LastName1"
            };

            // Act
            RedirectToRouteResult actual = target.Create(user) as RedirectToRouteResult;

            // Assert
            Assert.IsTrue(user.UserID != 0);
            Assert.IsTrue(target.ModelState.IsValid);

            Assert.IsNotNull(user.DateAdded);
            Assert.IsNull(user.DateModified);
            Assert.IsNull(user.IsDeleted);

            Assert.AreEqual(user.FirstName, newUser.FirstName);
            Assert.AreEqual(user.LastName, newUser.LastName);
            Assert.AreEqual(user.Email, newUser.Email);

            Assert.AreEqual("Index", actual.RouteValues["action"]);
        }
        public void TestCreate_WhenModelError_TooLongStrings_ReturnCreateView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            User user = new User()
            {
                Email = "*****@*****.**",
                FirstName = "longfirstnamelongfirstnamelongfirstnamelongfirstnamelongfirstname",
                LastName = "longlastnamelonglastnamelonglastnamelonglastnamelonglastnamelonglastname"
            };

            // Act
            ViewResult actual = target.Create(user) as ViewResult;

            // Assert
            Assert.IsFalse(target.ModelState.IsValid);
            Assert.IsTrue(user.UserID == 0);
            Assert.AreEqual("Create", actual.ViewName);
        }
        public void TestCreate_WhenModelError_InvalidEmail_ReturnCreateView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            User user = new User()
            {
                Email = "notvalid@[email protected]",
                FirstName = "FirstName1",
                LastName = "LastName1"
            };

            // Act
            ViewResult actual = target.Create(user) as ViewResult;

            // Assert
            Assert.IsFalse(target.ModelState.IsValid);
            Assert.IsTrue(user.UserID == 0);
            Assert.AreEqual("Create", actual.ViewName);
        }