Ejemplo n.º 1
0
        public void GetById_WhenCalled_ReturnsTheCorrectPlant()
        {
            var plantData = new List <Plant>
            {
                new Plant("plant1", 1, DateTime.UtcNow)
                {
                    Id = 1
                },
                new Plant("plant2", 2, DateTime.UtcNow)
                {
                    Id = 2
                },
            }.AsQueryable();

            var plantMockSet = new Mock <DbSet <Plant> >();

            plantMockSet.As <IQueryable <Plant> >().Setup(m => m.Provider).Returns(plantData.Provider);
            plantMockSet.As <IQueryable <Plant> >().Setup(m => m.Expression).Returns(plantData.Expression);
            plantMockSet.As <IQueryable <Plant> >().Setup(m => m.ElementType).Returns(plantData.ElementType);
            plantMockSet.As <IQueryable <Plant> >().Setup(m => m.GetEnumerator()).Returns(plantData.GetEnumerator());

            var mockContext = new Mock <PlantAppDbContext>();

            mockContext.Setup(m => m.Plants).Returns(plantMockSet.Object);
            var service = new PlantService(mockContext.Object);
            var plant   = service.GetById(1);

            Assert.AreEqual(1, plant.Id);
        }