コード例 #1
0
        public void MoqTest()
        {
            #region arrange
            var data = new List <General>
            {
                new General {
                    Name = "Hannibal Barca", Country = "Carthage", Comments = "Carthaginian general during the second Punic war.", Wiki_link = "https://en.wikipedia.org/wiki/Hannibal"
                },
                new General {
                    Name = "Pyhrrus", Country = "Epirus", Comments = "Fought in the Pyrrhic war, the term Pyrrhic victory is named after him.", Wiki_link = "https://en.wikipedia.org/wiki/Pyrrhus_of_Epirus"
                },
                new General {
                    Name = "Hamilcar Barca", Country = "Carthage", Comments = "Father of Hannibal Barca was a general of Carthage during the first Punic war gained the epithet Barca meaning lightning.", Wiki_link = "https://en.wikipedia.org/wiki/Hamilcar_Barca"
                },
                new General {
                    Name = "Flavius Aetius", Country = "Western Roman Empire", Comments = "Best known for his victory at the Battle of the Catalaunian Plains against Atilla the Hun.", Wiki_link = "https://en.wikipedia.org/wiki/Flavius_Aetius"
                },
                new General {
                    Name = "Subatai", Country = "Mongolia", Comments = "Mongolian general conquered more territory then any other general in history.", Wiki_link = "https://en.wikipedia.org/wiki/Subutai"
                },
                new General {
                    Name = "Khalid ibn al-Walid", Country = "Rashidun Caliphate", Comments = "Known for his Victories against the Byzantine and Sassanian Empires. He was undefeated.", Wiki_link = "https://en.wikipedia.org/wiki/Khalid_ibn_al-Walid"
                },
                new General {
                    Name = "Scipio Africanus", Country = "Rome", Comments = "Best known for his role in defeating Hannibal Barca during the second Punic war. Prior to that he campaigned against Carthage in modern day Spain and Portugal.", Wiki_link = "https://en.wikipedia.org/wiki/Scipio_Africanus"
                },
                new General {
                    Name = "Sulla", Country = "Rome", Comments = "Best known for his dictatorship over Rome. He also lead in wars against Mithridates and the Socii.", Wiki_link = "https://en.wikipedia.org/wiki/Sulla"
                }
            }.AsQueryable();
            Mock <DbSet <General> > mockSet = new Mock <DbSet <General> >();

            mockSet.As <IQueryable <General> >().Setup(x => x.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <General> >().Setup(x => x.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <General> >().Setup(x => x.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <General> >().Setup(x => x.GetEnumerator()).Returns(data.GetEnumerator());

            GeneralsController view = new GeneralsController
            {
                GenDbGenerals = mockSet.Object
            };
            #endregion
            #region access
            var resultView = view.Index();
            var result     = view.ViewData.Model as List <General>;

            #endregion
            #region assert
            var correctResult = data.OrderBy("ID " + ASCENDING_SORT).ToList();
            CollectionAssert.AreEqual(correctResult, result, "Moq test failed");
            #endregion
        }
コード例 #2
0
        public void IDTest()
        {
            Random         gen     = new Random();
            List <General> genList = db.Generals.ToList();
            int            testID  = gen.Next(genList.Count);
            ViewResult     v       = genController.Details(genList[testID].ID) as ViewResult;
            General        g       = v.Model as General;

            Assert.AreEqual(g, genList[testID], "Correct id is not being returned");

            //null check
            genController = new GeneralsController();
            HttpStatusCodeResult correctStatus = new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            ActionResult         details       = genController.Details(null);
            HttpStatusCodeResult resultStatus  = genController.Details(null) is HttpStatusCodeResult ? details as HttpStatusCodeResult : null;

            if (resultStatus == null)
            {
                Assert.Fail("not responding to nulls correctly");
            }
            else
            {
                Assert.AreEqual(correctStatus.StatusCode
                                , resultStatus.StatusCode, "Null check wrong status code");
            }

            //null general check
            genController = new GeneralsController();
            var nullGendetails       = genController.Details(-1);
            var nullGenDetailsResult = nullGendetails is HttpNotFoundResult ? nullGendetails as HttpNotFoundResult : null;

            if (nullGenDetailsResult == null)
            {
                Assert.Fail("did not handle non existent general properly");
            }
            else
            {
                var r = new HttpNotFoundResult();
                Assert.AreEqual(nullGenDetailsResult.StatusCode, r.StatusCode, "Did not handle non existent general");
            }
        }