public void can_get_animals_using_explicit_relationship_between_animal_and_enclosure()
        {
            using (var sandbox = new LocalDb())
            {
                Runner.MigrateToLatest(sandbox.ConnectionString);

                using (var context = new ZooDbContext(sandbox.ConnectionString))
                {
                    var enclosure = new Enclosure() { Id = 1, Name = "Kenya", Location = "Africa", Environment = "Sahara" };
                    var animal = new Animal() { Name = "Nala", Species = "Lion", EnclosureId = 1 };

                    context.Animals.Add(animal);
                    context.Enclosures.Add(enclosure);
                    context.SaveChanges();

                    var controller = new HomeController() { Database = context };

                    var result = controller.Index() as ViewResult;
                    var model = result == null ? new IndexViewModel() : result.Model as IndexViewModel;

                    Assert.Equal(1, model.Animals.Count());
                    Assert.Equal("Nala", model.Animals.First().AnimalName);
                }
            }
        }
        public void Animal_needs_an_enclosure()
        {
            var mockContext = new Mock<IZooDbContext>();

            var animals = new List<Animal>
            {
                new Animal
                {
                    Id = 1,
                    Name = "Pumba",
                    Enclosure = new Enclosure
                    {
                        Id = 1, Name = "Kenya Room",
                        Location = "Center",
                        Environment = "Africa"
                    }
                }
            }.AsQueryable();

            var mockSet = new Mock<DbSet<Animal>>();
            mockSet.As<IQueryable<Animal>>().Setup(m => m.Provider).Returns(animals.Provider);
            mockSet.As<IQueryable<Animal>>().Setup(m => m.Expression).Returns(animals.Expression);
            mockSet.As<IQueryable<Animal>>().Setup(m => m.ElementType).Returns(animals.ElementType);
            mockSet.As<IQueryable<Animal>>().Setup(m => m.GetEnumerator()).Returns(animals.GetEnumerator());

            mockContext.Setup(x => x.Animals)
                .Returns(mockSet.Object);

            var db = mockContext.Object;
            var controller = new HomeController() { Database = db };

            var result = controller.Index();

            Assert.IsType<ViewResult>(result);
        }
        public void Animal_needs_an_enclosure()
        {
            using (var db = new FakeZooDbContext())
            {
                db.Animals.Add(new Animal { Name = "Mufasa", EnclosureId = 1, Species = "Lion" });
                db.SaveChanges();

                var controller = new HomeController() {Database = db};

                Assert.Throws<NullReferenceException>(() => controller.Index());

                db.Enclosures.Add(new Enclosure());
                db.SaveChanges();

                Assert.Throws<NullReferenceException>(() => controller.Index());

                db.Animals.Find(1).Enclosure = new Enclosure();

                var result = controller.Index();

                Assert.IsType<ViewResult>(result);
            }
        }