コード例 #1
0
        public void TestEditCarCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White", Year = 2013, RentalPrice = 100, Description = "Just some car" };

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);

            viewModel.Cars = new ObservableCollection<Car>()
                {
                    car
                };

            Assert.IsTrue(viewModel.Cars[0].Color == "White");
            Assert.IsTrue(viewModel.CurrentCarViewModel == null);

            viewModel.EditCarCommand.Execute(car);

            Assert.IsTrue(viewModel.CurrentCarViewModel != null);

            mockServiceFactory.Setup(mock => mock.CreateClient<IInventoryService>().UpdateCar(It.IsAny<Car>())).Returns(viewModel.CurrentCarViewModel.Car);

            viewModel.CurrentCarViewModel.Car.Color = "Black";
            viewModel.CurrentCarViewModel.SaveCommand.Execute(null);

            Assert.IsTrue(viewModel.Cars[0].Color == "Black");
        }
コード例 #2
0
        public void TestCurrentCarSetting()
        {
            Car car = new Car() { CarId = 1 };

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);

            Assert.IsTrue(viewModel.CurrentCarViewModel == null);

            viewModel.EditCarCommand.Execute(car);

            Assert.IsTrue(viewModel.CurrentCarViewModel != null && viewModel.CurrentCarViewModel.Car.CarId == car.CarId);
        }
コード例 #3
0
        public void TestViewLoaded()
        {
            Car[] data = new List<Car>(){
                new Car() { CarId = 1 },
                new Car() { CarId = 2 }
            }.ToArray();

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();
            mockServiceFactory.Setup(mock => mock.CreateClient<IInventoryService>().GetAllCars()).Returns(data);

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);

            Assert.IsTrue(viewModel.Cars == null);

            object loaded = viewModel.ViewLoaded;

            Assert.IsTrue(viewModel.Cars != null && viewModel.Cars.Count == data.Length && viewModel.Cars[0] == data[0]);
        }
コード例 #4
0
        public void TestAddCarCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White" };

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);
            viewModel.Cars = new ObservableCollection<Car>();

            Assert.IsTrue(viewModel.CurrentCarViewModel == null);

            viewModel.AddCarCommand.Execute(car);

            Assert.IsTrue(viewModel.CurrentCarViewModel != null);

            mockServiceFactory.Setup(mock => mock.CreateClient<IInventoryService>().UpdateCar(It.IsAny<Car>())).Returns(viewModel.CurrentCarViewModel.Car);

            viewModel.CurrentCarViewModel.SaveCommand.Execute(null);

            Assert.IsTrue(viewModel.Cars != null && viewModel.Cars.Count == 1);
        }
コード例 #5
0
        public void TestDeleteCarCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White" };

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();
            mockServiceFactory.Setup(mock => mock.CreateClient<IRentalService>().IsCarCurrentlyRented(car.CarId)).Returns(false);
            mockServiceFactory.Setup(mock => mock.CreateClient<IInventoryService>().DeleteCar(car.CarId));

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);
            viewModel.Cars = new ObservableCollection<Car>()
                {
                    car
                };

            viewModel.ConfirmDelete += (s, e) => e.Cancel = false;

            Assert.IsTrue(viewModel.Cars.Count == 1);

            viewModel.DeleteCarCommand.Execute(car);

            Assert.IsTrue(viewModel.Cars.Count == 0);
        }
コード例 #6
0
        public void TestDeleteCarCommandWithError()
        {
            Car car = new Car() { CarId = 1, Color = "White" };

            Mock<IServiceFactory> mockServiceFactory = new Mock<IServiceFactory>();
            mockServiceFactory.Setup(mock => mock.CreateClient<IRentalService>().IsCarCurrentlyRented(car.CarId)).Returns(true); // currently rented
            mockServiceFactory.Setup(mock => mock.CreateClient<IInventoryService>().DeleteCar(car.CarId));

            MaintainCarsViewModel viewModel = new MaintainCarsViewModel(mockServiceFactory.Object);
            viewModel.Cars = new ObservableCollection<Car>()
            {
                car
            };

            bool errorOccured = false;
            viewModel.ErrorOccured += (s, e) => errorOccured = true;

            Assert.IsTrue(viewModel.Cars.Count == 1);

            viewModel.DeleteCarCommand.Execute(car);

            Assert.IsTrue(errorOccured && viewModel.Cars.Count == 1);
        }