public void TestSaveCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White", Description = "Kia Optima", Year = 2013, RentalPrice = 149.00M };

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

            EditCarViewModel viewModel = new EditCarViewModel(mockServiceFactory.Object, car);

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

            viewModel.Car.Color = "Black";

            bool carUpdated = false;
            string color = string.Empty;
            viewModel.CarUpdated += (s, e) =>
            {
                carUpdated = true;
                color = e.Car.Color;
            };

            viewModel.SaveCommand.Execute(null);

            Assert.IsTrue(carUpdated);
            Assert.IsTrue(color == "Black");
        }
        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");
        }
        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);
        }
        public void TestCarIsValid()
        {
            Car car = new Car() { CarId = 1, Color = "White", Description = "Kia Optima", Year = 2013 };

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

            EditCarViewModel viewModel = new EditCarViewModel(mockServiceFactory.Object, car);

            Assert.IsTrue(!viewModel.Car.IsValid);

            viewModel.Car.RentalPrice = 149;

            Assert.IsTrue(viewModel.Car.IsValid);
        }
        public void TestCanSaveCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White", Description = "Kia Optima", Year = 2013, RentalPrice = 149 };

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

            EditCarViewModel viewModel = new EditCarViewModel(mockServiceFactory.Object, car);

            Assert.IsFalse(viewModel.SaveCommand.CanExecute(null));

            viewModel.Car.Color = "Black";

            Assert.IsTrue(viewModel.SaveCommand.CanExecute(null));
        }
Ejemplo n.º 6
0
        // note that this viewmodel is instantiated on-demand from parent and not with DI
        public EditCarViewModel(IServiceFactory serviceFactory, Car car)
        {
            _ServiceFactory = serviceFactory;
            _Car = new Car()
            {
                CarId = car.CarId,
                Description = car.Description,
                Color = car.Color,
                Year = car.Year,
                RentalPrice = car.RentalPrice
            };

            _Car.CleanAll();

            SaveCommand = new DelegateCommand<object>(OnSaveCommandExecute, OnSaveCommandCanExecute);
            CancelCommand = new DelegateCommand<object>(OnCancelCommandExecute);
        }
        public void TestCancelCommand()
        {
            Car car = new Car() { CarId = 1, Color = "White" };

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

            EditCarViewModel viewModel = new EditCarViewModel(mockServiceFactory.Object, car);

            bool canceled = false;
            viewModel.CancelEditCar += (s, e) => canceled = true;

            Assert.IsTrue(!canceled);

            viewModel.CancelCommand.Execute(null);

            Assert.IsTrue(viewModel.CancelCommand.CanExecute(null));

            Assert.IsTrue(canceled);
        }
        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);
        }
        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);
        }
Ejemplo n.º 10
0
 private void OnEditCarCommand(Car car)
 {
     if (car != null)
     {
         CurrentCarViewModel = new EditCarViewModel(_ServiceFactory, car);
         CurrentCarViewModel.CarUpdated += CurrentCarViewModel_CarUpdated;
         CurrentCarViewModel.CancelEditCar += CurrentCarViewModel_CancelEditCar;
     }
 }
Ejemplo n.º 11
0
        private void OnDeleteCarCommand(Car car)
        {
            bool carIsRented = false;

            // check to see if car is currently rented
            WithClient<IRentalService>(_ServiceFactory.CreateClient<IRentalService>(), rentalClient =>
            {
                carIsRented = rentalClient.IsCarCurrentlyRented(car.CarId);
            });

            if (!carIsRented)
            {
                CancelEventArgs args = new CancelEventArgs();
                if (ConfirmDelete != null)
                    ConfirmDelete(this, args);

                if (!args.Cancel)
                {
                    try
                    {
                        WithClient<IInventoryService>(_ServiceFactory.CreateClient<IInventoryService>(), inventoryClient =>
                        {
                            inventoryClient.DeleteCar(car.CarId);
                            _Cars.Remove(car);
                        });
                    }
                    catch (FaultException ex)
                    {
                        if (ErrorOccured != null)
                            ErrorOccured(this, new ErrorMessageEventArgs(ex.Message));
                    }
                    catch (Exception ex)
                    {
                        if (ErrorOccured != null)
                            ErrorOccured(this, new ErrorMessageEventArgs(ex.Message));
                    }
                }
            }
            else
            {
                if (ErrorOccured != null)
                    ErrorOccured(this, new ErrorMessageEventArgs("Cannot delete this car. It is currently rented"));
            }
        }
Ejemplo n.º 12
0
        private void OnAddCarCommand(object arg)
        {
            Car car = new Car();

            CurrentCarViewModel = new EditCarViewModel(_ServiceFactory, car);
            CurrentCarViewModel.CarUpdated += CurrentCarViewModel_CarUpdated;
            CurrentCarViewModel.CancelEditCar += CurrentCarViewModel_CancelEditCar;
        }
Ejemplo n.º 13
0
 public CarEventArgs(Car car, bool isNew)
 {
     Car = car;
     IsNew = isNew;
 }
        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);
        }
        public void TestViewModelConstruction()
        {
            Car car = new Car() { CarId = 1, Color = "White" };

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

            EditCarViewModel viewModel = new EditCarViewModel(mockServiceFactory.Object, car);

            Assert.IsTrue(viewModel.Car != null && viewModel.Car != car);
            Assert.IsTrue(viewModel.Car.CarId == car.CarId && viewModel.Car.Color == car.Color);
        }