public void CreatActionRedirectsToIndexOnValid()
        {
            // Arrange
            // create the mock
            var mockRepository = new Mock<IUserRepository>();  // don't actually need repository in this test

            User user = new User
            {
                username = "******",
                firstname = "fn1",
                lastname = "ln1",
                password = "******",
                address = "ad1",
                datejoined = DateTime.Now
            };

            // Save is void - do not specify any behaviour  for now
            mockRepository.Setup(r => r.Save(It.IsAny<User>()));

            // pass the mocked instance, not the mock itself, to the category
            // controller using the Object property
            var controller = new UserController(mockRepository.Object);

            // Act
            var result = controller.Create(user) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result, "Incorrect result type");
            Assert.AreEqual("Index", result.RouteValues["action"],
                "Incorrect action in redirect");
        }
 public ActionResult Create(User user)
 {
     if (ModelState.IsValid)
     {
         repository.Save(user);
         return RedirectToAction("Index");
     }
     else
     {
         return View();
     }
 }
 public void Save(User user)
 {
     context.Users.Add(user);
     context.SaveChanges();
 }
        public void CreatActionRedisplaysViewOnInvalid()
        {
            // Arrange
            // create the mock
            var mockRepository = new Mock<IUserRepository>();  // don't actually need repository in this test

            User user = new User();

            // Save is void - do not specify any behaviour  for now
            mockRepository.Setup(r => r.Save(It.IsAny<User>()));

            // pass the mocked instance, not the mock itself, to the
            // controller using the Object property
            var controller = new UserController(mockRepository.Object);

            // introduce a model state error
            controller.ModelState.AddModelError("key", "model is invalid");

            // Act
            var result = controller.Create(user) as ViewResult;

            // Assert
            Assert.IsNotNull(result, "Incorrect result type");
            Assert.AreEqual("", result.ViewName, "Incorrect view");
        }