Exemplo n.º 1
0
        public void EditView_True()
        {
            // Arrange - create the controller
            GreetingController target = new GreetingController(mock.Object);

            // Act - call the action method
            var result = target.Edit(2) as ViewResult;

            // Assert - check the result
            Assert.AreEqual("", result.ViewName);
        }
Exemplo n.º 2
0
        public void EditGet_CanEdit_ValidGreeting()
        {
            // Arrange - create the controller
            GreetingController target = new GreetingController(mock.Object);

            // Act - call the action method
            var result = target.Edit(2) as ViewResult;

            // Assert - check the result
            Assert.IsInstanceOf(typeof(ViewResult), result);
            Assert.AreEqual("", result.ViewName);
            Assert.IsNotNull(result.ViewData.Model);
        }
Exemplo n.º 3
0
        public void EditGet_CannotEdit_InvalidGreeting()
        {
            // Arrange - create the controller
            GreetingController target = new GreetingController(mock.Object);

            // Act - call the action method
            var      result   = (HttpNotFoundResult)target.Edit(15);
            Greeting Greeting = mock.Object.Greetings.Where(m => m.GreetingId == 1500).FirstOrDefault();

            // Assert - check the result
            Assert.IsInstanceOf(typeof(HttpNotFoundResult), result);
            Assert.IsNull(Greeting);
            Assert.AreEqual(404, result.StatusCode);
        }
Exemplo n.º 4
0
        public void EditPost_CannotEdit_InvalidGreeting()
        {
            // Arrange - create the controller
            Greeting Greeting         = new Greeting {
            };
            GreetingController target = new GreetingController(mock.Object);

            // Act - call the action method
            target.ModelState.AddModelError("error", "error");
            var    result = target.Edit(Greeting);
            string data   = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(((JsonResult)result).Data, "error")).Target;

            // Assert - check the result
            mock.Verify(m => m.SaveGreeting(Greeting), Times.Never);
            Assert.IsInstanceOf(typeof(JsonResult), result);
            Assert.AreEqual("", data);
            //Assert.IsInstanceOf(typeof(Greeting), result.ViewData.Model);
        }
Exemplo n.º 5
0
        public void EditPost_ValidModelConcurrency_ErrorReturned()
        {
            //Arrange
            GreetingController controller = new GreetingController(mock.Object);

            mock.Setup(m => m.SaveGreeting(It.IsAny <Greeting>())).Throws(new DbUpdateConcurrencyException());
            string modelError = "The record you attempted to edit "
                                + "was modified by another user after you got the original value. The "
                                + "edit operation was canceled.";

            //Act
            JsonResult result = (JsonResult)controller.Edit(mock.Object.Greetings.FirstOrDefault());
            string     data   = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(result.Data, "error")).Target;

            //Assert
            mock.Verify(d => d.SaveGreeting(It.IsAny <Greeting>()), Times.Once());
            Assert.AreEqual(typeof(JsonResult), result.GetType());
            Assert.AreEqual(modelError, data);
        }
Exemplo n.º 6
0
        public void EditPost_CanEdit_ValidGreeting()
        {
            // Arrange - create the controller
            GreetingController target   = new GreetingController(mock.Object);
            Greeting           Greeting = new Greeting {
                GreetingId = 6, GreetingHeader = "Greeting 1", GreetingBody = "Test Greeting"
            };


            // Act - call the action method
            var result = (RedirectToRouteResult)target.Edit(Greeting);

            // Assert - check the result
            Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
            Assert.IsFalse(result.Permanent);
            Assert.AreEqual("Home", result.RouteValues["controller"]);
            Assert.AreEqual("BDMView", result.RouteValues["action"]);
            mock.Verify(m => m.SaveGreeting(Greeting), Times.Once);
        }