public void GetAnimals_ValidData_ContainsExpectedAnimals() { var document = new XDocument( new XElement("Zoo", new XElement("Lions", new XElement("Lion", new XAttribute("name", "Simba"), new XAttribute("kg", 160) ) ), new XElement("Tigers", new XElement("Tiger", new XAttribute("name", "Dante"), new XAttribute("kg", 150) ) ) ) ); var mockFileReader = new Mock <IXmlDocumentReader>(); mockFileReader.Setup(m => m.GetXDocument(It.IsAny <string>())).Returns(document); var zooRepository = new ZooRepository(@"c:\temp\RateRepository.txt", mockFileReader.Object); var animalsList = zooRepository.GetAnimals().ToList(); var animal1 = animalsList.ElementAt(0); Assert.AreEqual("Simba", animal1.Name); Assert.AreEqual(160, animal1.Weight); var animal2 = animalsList.ElementAt(1); Assert.AreEqual("Dante", animal2.Name); Assert.AreEqual(150, animal2.Weight); }
public void CanIGetMyAnimals() { //Create your Mock DbSets that will be used for testing. Mock <DbSet <Animal> > mockAnimals; //Create a list for your DbSet that will be used for testing. List <Animal> AnimalList = new List <Animal>() { new Animal { Name = "Rex", BirthDate = new DateTime(2020, 10, 2) } }; myContext = new Mock <ZooContext>(); ZooRepository repo = new ZooRepository(myContext.Object); mockAnimals = new Mock <DbSet <Animal> >(); ConnectingToDatabase(); List <Animal> myanimals = repo.GetAnimals(); Assert.AreEqual(myanimals.Count, 1); }
// GET: Animal public ActionResult Index() { ViewBag.Animals = repo.GetAnimals(); return(View()); }