Example #1
0
        public void VerifyAddModelValidation_EmptyName()
        {
            var model = new AddMyObjectModel {
                Id = Guid.NewGuid(), Name = null, Created = new DateTime(2015, 2, 21)
            };
            var results = new List <ValidationResult>();

            Assert.False(Validator.TryValidateObject(model, new ValidationContext(model), results));
        }
Example #2
0
        public void VerifySendingAValidModelToAddMyObjectFormSavesTheObject()
        {
            var mockBLL = new Mock <IMyObjectLogic>();

            mockBLL.Setup(bll => bll.AddMyObject(It.IsAny <MyObject>()));
            var controller = new SolTech.Demos.UnitTesting.Controllers.HomeController(mockBLL.Object);
            var model      = new AddMyObjectModel {
                Id = Guid.NewGuid(), Name = "UnitTest", Created = new DateTime(2015, 2, 21)
            };
            var result = controller._AddObjectForm(model);

            // Verify that the method was called with the expected object
            mockBLL.Verify(bll => bll.AddMyObject(It.Is <MyObject>(item => item.Id == model.Id && item.Name == model.Name && item.Created == model.Created)));
        }
Example #3
0
        public void VerifySendingAnInvalidModelToAddMyObjectFormDoesNotSaveTheObject()
        {
            var mockBLL = new Mock <IMyObjectLogic>();

            mockBLL.Setup(bll => bll.AddMyObject(It.IsAny <MyObject>()));
            var controller = new SolTech.Demos.UnitTesting.Controllers.HomeController(mockBLL.Object);

            controller.ModelState.AddModelError("Guid", "some error");
            var model = new AddMyObjectModel {
                Id = Guid.Empty, Name = "UnitTest", Created = new DateTime(2015, 2, 21)
            };
            var result = controller._AddObjectForm(model);

            // Verify that the method was never called
            mockBLL.Verify(bll => bll.AddMyObject(It.IsAny <MyObject>()), Times.Never);
        }