示例#1
0
        public void GetEnginesByPower()
        {
            var data = new List <Engine>
            {
                new Engine {
                    Name = "Engine1", Power = 150
                },
                new Engine {
                    Name = "Engine2", Power = 150
                },
                new Engine {
                    Name = "Engine3", Power = 100
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Engine> >();

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

            var mockContext = new Mock <CarDealershipContext>();

            mockContext.Setup(m => m.Engines).Returns(mockSet.Object);

            var service      = new EngineBusiness(mockContext.Object);
            var EnginesFound = service.GetEnginesByPower(150);

            Assert.AreEqual(2, EnginesFound.Count());
            Assert.AreEqual("Engine1", EnginesFound[0].Name);
            Assert.AreEqual("Engine2", EnginesFound[1].Name);
        }
        /// <summary>
        /// Populate the data grid with information about engine with given power
        /// </summary>
        private void PopulateDataGridViewGetEnginesByPower()
        {
            dataGridView.Rows.Clear();
            EngineBusiness engineBusiness = new EngineBusiness();

            int.TryParse(txtGet.Text, out int power);
            var enginesList = engineBusiness.GetEnginesByPower(power);

            DataPopulator(enginesList);
        }