Example #1
0
        public void Commodity_Controller_Index_Test()
        {
            //this three lines are good for testing null pointer exception validations
            //Mock<IUnitOfWork> mockCommodityRepository = new Mock<IUnitOfWork>();
            //CommodityController target = new CommodityController(mockCommodityRepository.Object); // TODO: Initialize to an appropriate value
            // mockCommodityRepository.Object.GetAll();//SetupGet(d => d.GetAllParents()).SetupGet(d => d.GetAll()).Returns(mockCommodityRepository.Object.GetAll());

            IUnitOfWork         repository = this.MockCommodityRepository;
            CommodityController target     = new CommodityController(repository);
            ActionResult        expected   = new ViewResult();


            //act call the index will call getall() from the repo and returns a list of comods
            ActionResult actual = target.Index();


            ViewResult result = actual as ViewResult;

            Assert.IsNotNull(result);

            Assert.IsInstanceOfType(result.ViewData.Model, typeof(IEnumerable <Commodity>));

            Assert.IsNull(result.ViewBag.Title);
            Assert.IsNotNull(result.ViewBag.ParentID);
            Assert.IsNotNull(result.ViewBag.SelectedCommodityID);
            Assert.IsNotNull(result.ViewBag.Parents);


            //just for testing that we are really using the test model objects
            var count = result.ViewData.Model as IEnumerable <Commodity>;

            Assert.AreEqual(count.Count(), 5);
        }
        public void CanViewIndex()
        {
            //ACT
            var result = _commodityController.Index() as ViewResult;

            Assert.IsNotNull(result);
            var model = result.Model;

            //ASSERT
            Assert.IsInstanceOf <IEnumerable <CommodityType> >(result.ViewBag.CommodityTypes);
            Assert.IsInstanceOf <SelectList>(result.ViewBag.ParentID);
            Assert.IsInstanceOf <Int32>(result.ViewBag.SelectedCommodityID);
            Assert.IsInstanceOf <IOrderedEnumerable <Commodity> >(result.ViewBag.Parents);
            Assert.IsInstanceOf <IEnumerable <Commodity> >(model);
            Assert.AreEqual(2, ((IOrderedEnumerable <Commodity>)model).Count());
        }