Ejemplo n.º 1
0
        public void CarsRepositorySortedByMake_ShouldReturnSortedCollection(int numberOfCars)
        {
            var collection = new List <Car>();
            var rng        = new Random();

            // make car with random make
            for (int i = 0; i < numberOfCars; i++)
            {
                var makeString = new string(((char)(65 + rng.Next(26))), rng.Next(10));
                makeString = (i % 2) == 0 ? makeString : makeString.ToUpper();

                var car = new CarChild(make: makeString);
                collection.Add(car);
            }

            var mockDb = new Mock <IDatabase>();

            mockDb.Setup(x => x.Cars).Returns(collection);

            var repo = new CarsRepository(mockDb.Object);

            var expected = collection
                           .OrderBy(x => x.Make)
                           .ToList();

            CollectionAssert.AreEqual(expected, repo.SortedByMake());
        }
Ejemplo n.º 2
0
        public void AddCar_ShouldThrowArgumentNullException_WhenCalledWithCarWithMakeNullOrEmpty(string make)
        {
            var car = new CarChild(make: make, model: "valid model");

            var controller = new CarsController();

            Assert.Throws <ArgumentNullException>(() => controller.Add(car));
        }
Ejemplo n.º 3
0
        public void AddCar_ShouldThrowArgumentNullException_WhenCalledWithCarWithModelNullOrEmpty(string model)
        {
            var car = new CarChild(make: "actual make", model: model);

            var controller = new CarsController();

            Assert.Throws <ArgumentNullException>(() => controller.Add(car));
        }
Ejemplo n.º 4
0
        public void AddCar_ShouldReturnIView_IfCarIsValid()
        {
            var car = new CarChild(5, "make", "model", 2000);

            var mockRepo = new Mock <ICarsRepository>();

            mockRepo.Setup(x => x.GetById(5)).Returns(car);

            var controller = new CarsController(mockRepo.Object);

            Assert.IsInstanceOf <IView>(controller.Add(car));
        }
Ejemplo n.º 5
0
        public void Details_ShouldReturnIview_IfCarIdIsValid()
        {
            var expectedId = 444;
            var carKid     = new CarChild(expectedId, "6", "7", 2000);

            var mockedRepo = new Mock <ICarsRepository>();

            mockedRepo.Setup(x => x.GetById(It.IsAny <int>())).Returns(carKid);

            var controller = new CarsController(mockedRepo.Object);

            Assert.IsInstanceOf <IView>(controller.Details(expectedId));
        }
Ejemplo n.º 6
0
        public void Details_ShouldThrowArgumentNullException_IfCarIsNull()
        {
            Car car    = null;
            var carKid = new CarChild(5, "6", "7", 2000);

            var mockedRepo = new Mock <ICarsRepository>();

            mockedRepo.Setup(x => x.GetById(It.IsAny <int>())).Returns(car);

            var controller = new CarsController(mockedRepo.Object);

            Assert.Throws <ArgumentNullException>(() => controller.Add(carKid));
        }
Ejemplo n.º 7
0
        public void AddCar_ShouldCallRepositoryGetByIdWithSameIDPassed_IfCarIsValid()
        {
            var expectedId = 8;
            var car        = new CarChild(id: expectedId, make: "make", model: "model");

            var mockedRepo = new Mock <ICarsRepository>();

            mockedRepo.Setup(x => x.GetById(expectedId)).Returns(car).Verifiable();

            var controller = new CarsController(mockedRepo.Object);

            controller.Add(car);

            mockedRepo.Verify(x => x.GetById(expectedId), Times.Once);
        }
Ejemplo n.º 8
0
        public void AddCar_ShouldCallRepositoryAddCar_WithCarPassed()
        {
            var car = new CarChild(id: 5, make: "make", model: "model");

            var mockedRepo = new Mock <ICarsRepository>();

            mockedRepo.Setup(x => x.Add(car)).Verifiable();
            mockedRepo.Setup(x => x.GetById(5)).Returns(car);

            var controller = new CarsController(mockedRepo.Object);

            controller.Add(car);

            mockedRepo.Verify(x => x.Add(car), Times.Once);
        }
Ejemplo n.º 9
0
        public void CarRepositoryRemove_ShouldRemoveCarProperly()
        {
            var car = new CarChild();

            var collection = new List <Car>();

            var mockDb = new Mock <IDatabase>();

            mockDb.Setup(x => x.Cars).Returns(collection);

            var repo = new CarsRepository(mockDb.Object);

            var contains = collection.Contains(car);

            Assert.IsFalse(contains);
        }
Ejemplo n.º 10
0
        public void CarsRepositoryAdd_ShouldAddCarProperly()
        {
            var car = new CarChild();

            var collection = new List <Car>();

            var mockDb = new Mock <IDatabase>();

            mockDb.Setup(x => x.Cars).Returns(collection);

            var repo = new CarsRepoChild(mockDb.Object);

            repo.Add(car);

            Assert.Contains(car, collection);
        }
Ejemplo n.º 11
0
        public void CarsRepositorySortByYear_ShouldRetuenSortedCollection(int numberOfCars)
        {
            var collection = new List <Car>();
            var rng        = new Random();

            for (int i = 0; i < numberOfCars; i++)
            {
                var car = new CarChild(year: 1980 + rng.Next(10));
                collection.Add(car);
            }

            var mockDb = new Mock <IDatabase>();
            var repo   = new CarsRepository(mockDb.Object);

            mockDb.Setup(x => x.Cars).Returns(collection);



            var expected = collection
                           .OrderByDescending(x => x.Year)
                           .ToList();

            CollectionAssert.AreEqual(expected, repo.SortedByYear());
        }