コード例 #1
0
        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");
        }
コード例 #2
0
 public ActionResult Create(User user)
 {
     if (ModelState.IsValid)
     {
         repository.Save(user);
         return RedirectToAction("Index");
     }
     else
     {
         return View();
     }
 }
コード例 #3
0
 public void Save(User user)
 {
     context.Users.Add(user);
     context.SaveChanges();
 }
コード例 #4
0
        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");
        }