示例#1
0
        public void DetailsTest()
        {
            //Arrange
            var PieId = Guid.NewGuid();

            var _pie = new Pie
            {
                PieId            = PieId,
                PieName          = "Apple Pie",
                Price            = 10,
                ShortDescreption = "UnitTestPie",
                LongDescreption  = "UnitTestPie",
                PiesOfTheWeek    = true
            };

            //this tells the mock repository when an invoke of GetPieByID with PieID return _pie
            _PieRepository.Setup(expression: p => p.GetPieById(PieId)).Returns(_pie);

            const string firstExpected  = "Details";
            const string secondExpected = "NotFoundAction";

            //Act
            var firstResult  = _PieController.Details(PieId) as ViewResult;
            var secondResult = _PieController.Details(Guid.NewGuid()) as RedirectToActionResult;

            //Assert
            Assert.AreEqual(firstExpected, firstResult.ViewName);
            Assert.AreEqual(secondExpected, secondResult.ActionName);
        }
        public void Get_Pie_Details()
        {
            // Arrange

            var pie = new Pie
            {
                PieId            = 1,
                Name             = "Strawberry Pie",
                Price            = 15.95M,
                ShortDescription = "Lorem Ipsum"
            };

            var pieRepo = new Mock <IPieRepository>();

            pieRepo.Setup(c => c.GetPieById(It.IsAny <int>())).Returns(pie);
            var categoryRepo = new Mock <ICategoryRepository>();

            var controller = new PieController(pieRepo.Object, categoryRepo.Object);
            // Act

            var result = controller.Details(It.IsAny <int>());

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <Pie>(
                viewResult.Model);

            Assert.Equal("Strawberry Pie", model.Name);
        }