public void All_ShouldReturnAllOfTheCarsInTheDB() { // Arrange var firstCar = new Car { Id = 1, Make = "Audi", Model = "A5", Year = 2005 }; var secondCar = new Car { Id = 2, Make = "Moskvich", Model = "12-tak", Year = 1980 }; var thirdCar = new Car { Id = 3, Make = "Lada", Model = "7-marka", Year = 1991 }; var mock = new Mock <IDatabase>(); mock.Setup(m => m.Cars).Returns(new List <Car>() { }); var fakeDB = mock.Object; fakeDB.Cars.Add(firstCar); fakeDB.Cars.Add(secondCar); fakeDB.Cars.Add(thirdCar); var carsRepository = new CarsRepository(fakeDB); // Act var result = carsRepository.All(); //Assert Assert.AreEqual(3, result.Count); }
public void TestCarsRepository_AllMethod_ShouldWorkCorrectly() { var carRepo = new CarsRepository(); foreach (var car in cars) { carRepo.Add(car); } Assert.AreEqual(cars, carRepo.All()); }
public void CarsRepositoryAll_shouldProperslyReturnResult() { var Collection = new List <ICar>(); var car = Mock.Create <ICar>(); Collection.Add(car); var mockDb = Mock.Create <IDatabase>(); Mock.Arrange(() => mockDb.Cars).Returns(Collection); var repo = new CarsRepository(mockDb); var expectedList = new List <ICar>(Collection); bool areEquals = expectedList.TrueForAll(x => repo.All().Contains(x)); Assert.IsTrue(areEquals); }
public void CarsRepositoryAll_ShouldReturnListOfCarsProperly() { int numberOfCars = 7; var collection = new List <Car>(); for (int i = 0; i < numberOfCars; i++) { collection.Add(new CarChild(id: i)); } var mockDb = new Mock <IDatabase>(); mockDb.Setup(x => x.Cars).Returns(collection); var repo = new CarsRepository(mockDb.Object); var result = (List <Car>)repo.All(); CollectionAssert.AreEqual(collection, result); }
private static void Module1IOC() { var builder = new ContainerBuilder(); builder.RegisterType <ProjectContext>() .As <DbContext>(); builder.RegisterGeneric(typeof(BaseRepository <>)) .As(typeof(IRepository <>)) .InstancePerLifetimeScope(); using (var container = builder.Build()) { //ICarsRepository<Cars> carsRepo = container.Resolve<ICarsRepository<Cars>>(); var carsRepo = new CarsRepository(new ProjectContext()); var cars = carsRepo.All().ToList(); var car = carsRepo.GetById(1001); //一直只能点出ToListAsync方法是因为没有引入System.Linq //ToListAsync是EF中的扩展方法 //var cars = carsRepo.All().ToListAsync(); } }