示例#1
0
        public void TestSearchAll()
        {
            const string   search        = "Barca";
            const string   sort          = "asc";
            List <General> correctResult = FullSearch(search, sort);
            var            view          = genController.Index(SearchString: search, SearchName: true, SearchCountry: true, SearchComments: true) as ViewResult;

            List <General> resultList = (List <General>)view.ViewData.Model;

            Assert.IsTrue(resultList.All(x =>
                                         x.Country.Contains(search) || x.Name.Contains(search) || x.Comments.Contains(search)));
            General g = resultList.Find(x => !(x.Country.Contains(search) || x.Name.Contains(search) || x.Comments.Contains(search)));

            if (g != null)
            {
                Assert.Fail(g.Name + "incorrect general found");
            }
            CollectionAssert.AreEqual(correctResult, resultList, "The searchall list is not correct ");
        }
        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
        }