예제 #1
0
        static void Main(string[] args)
        {
            var carController          = new CarsController();
            var detailController       = new DetailsController();
            var manufacturerController = new ManufacturerController();
            var manufacturerService    = new ManufacturerService();
            //var allCars = carController.GetAll();
            //var carById = carController.GetId(1);
            var allDetails = detailController.GetAll();

            //var detailById = detailController.GetId(2);
            //detailController.Insert(null);
            //detailController.Update(null);
            //detailController.Delete(1002);


            carController.Add(null);
            //carController.Update(null);
            //carController.Delete(6);

            var allManufacturers = manufacturerController.GetAll();
            //var richcars = manufacturerController.GetRich();
            var mostexpensive = manufacturerService.GetMostExpensive();

            Console.ReadKey();
        }
예제 #2
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));
        }
예제 #3
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));
        }
예제 #4
0
        public void Add_ShouldThrowArgumentNullException_WhenCarIsNull()
        {
            var mockedRepo = Mock.Create <ICarsRepository>();

            var controller = new CarsController(mockedRepo);

            Assert.ThrowsException <ArgumentNullException>(() => controller.Add(null));
        }
예제 #5
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));
        }
예제 #6
0
        public void Add_ShouldThrowArgumentNullException_WhenCarMakeIsEmpty()
        {
            var car = Mock.Create <ICar>();

            Mock.Arrange(() => car.Model).Returns("valid");
            Mock.Arrange(() => car.Make).Returns("");

            var mockedRepo = Mock.Create <ICarsRepository>();

            var controller = new CarsController(mockedRepo);

            Assert.ThrowsException <ArgumentNullException>(() => controller.Add(car));
        }
예제 #7
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));
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
        public void Add_ShouldCallRepoCall_WhenCarIsValid()
        {
            var mockedCar = Mock.Create <ICar>();

            Mock.Arrange(() => mockedCar.Make).Returns("make");
            Mock.Arrange(() => mockedCar.Model).Returns("model");

            var mockedRepo = Mock.Create <ICarsRepository>();

            Mock.Arrange(() => mockedRepo.Add(mockedCar)).OccursOnce();

            var controller = new CarsController(mockedRepo);

            controller.Add(mockedCar);

            Mock.Assert(mockedRepo);
        }
예제 #11
0
        public void Create_ReturnsBadRequest_GivenInvalidCar()
        {
            // Arrange & Act
            var mockService = new Mock <ICarService>();

            var controller = new CarsController(mockService.Object);

            mockService.Setup(repo => repo.Add(null))
            .Returns(new ErrorResult());

            controller.ModelState.AddModelError("error", "some error");

            // Act
            var result = controller.Add(car: null);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
예제 #12
0
        public void Add_CarParameterMakeOrModelAreNull_ShouldThrowTheCorrectExceptionMessage()
        {
            var actual = false;

            Car car        = new FakeCar();
            var controller = new CarsController();

            try
            {
                controller.Add(car);
            }
            catch (ArgumentNullException ex)
            {
                actual = ex.Message.Contains("Car make and model cannot be empty");
            }

            Assert.IsTrue(actual);
        }
예제 #13
0
        public void Create_ReturnsOkResult_GivenInvalidCar()
        {
            // Arrange & Act
            var testCar = GetTestCar().Data;

            var mockService = new Mock <ICarService>();

            var controller = new CarsController(mockService.Object);

            mockService.Setup(repo => repo.Add(testCar))
            .Returns(new SuccessResult());

            // Act
            var result = controller.Add(testCar);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
예제 #14
0
        public void Add_CarParameterIsNull_ShouldThrowTheCorrectExceptionMessage()
        {
            var actual = false;

            Car car        = null;
            var controller = new CarsController();

            try
            {
                controller.Add(car);
            }
            catch (ArgumentNullException ex)
            {
                actual = ex.Message.Contains("Car cannot be null");
            }

            Assert.IsTrue(actual);
        }
예제 #15
0
        public void Add_ShouldReturnIView_WhenCarIsValid()
        {
            var hardcodedId = 5;

            var mockedCar = Mock.Create <ICar>();

            Mock.Arrange(() => mockedCar.Id).Returns(hardcodedId);
            Mock.Arrange(() => mockedCar.Make).Returns("make");
            Mock.Arrange(() => mockedCar.Model).Returns("model");

            var mockedRepo = Mock.Create <ICarsRepository>();

            Mock.Arrange(() => mockedRepo.GetById(hardcodedId)).Returns(mockedCar);

            var controller = new CarsController(mockedRepo);

            Assert.IsInstanceOfType(controller.Add(mockedCar), typeof(IView));
        }
예제 #16
0
        public void Add_ShouldAccessICarsRepositoryAddMethod()
        {
            var car = new Car();

            car.Make  = "asd";
            car.Model = "ass";
            car.Id    = 1;

            var fakeCarRepo = new Mock <ICarsRepository>();

            fakeCarRepo.Setup(cp => cp.GetById(It.IsAny <int>())).Returns(car);
            fakeCarRepo.Setup(cp => cp.Add(It.IsAny <Car>()));

            var controller = new CarsController(fakeCarRepo.Object);

            var actual = controller.Add(car);

            fakeCarRepo.Verify(repo => repo.Add(It.IsAny <Car>()), Times.Exactly(1));
        }
예제 #17
0
        public void Add_ShouldAccessICarsRepositoryAddMethod()
        {
            // Arrange
            var car = new Car();

            car.Make  = "Mazda";
            car.Model = "MX-7";
            car.Id    = 1;

            var fakeCarRepo = new Mock <ICarsRepository>();

            fakeCarRepo.Setup(z => z.GetById(It.IsAny <int>())).Returns(car);
            fakeCarRepo.Setup(z => z.Add(It.IsAny <Car>()));

            var controller = new CarsController(fakeCarRepo.Object);

            // Act
            var actual = controller.Add(car);

            // Assert
            fakeCarRepo.Verify(repo => repo.Add(It.IsAny <Car>()), Times.Exactly(1));
        }
예제 #18
0
        public void AddCar_ShouldThrowArgumentNullException_WhenCalledWithNull()
        {
            var controller = new CarsController();

            Assert.Throws <ArgumentNullException>(() => controller.Add(null));
        }