public void Put_UpdatesVehicleProperties()
        {
            var vehicles = new List <Vehicle>
            {
                new Vehicle {
                    Id = 1, RegistrationNumber = "1"
                },
            }.AsQueryable();
            var vehiclesMockSet = MockDbSetHelper.CreateMockSet(vehicles);
            var mockContext     = new Mock <InstantDeliveryContext>();

            mockContext.Setup(c => c.Vehicles).Returns(vehiclesMockSet.Object);

            var controller = new VehiclesController(mockContext.Object);
            var selected   = vehiclesMockSet.Object.FirstOrDefault();

            vehiclesMockSet.Object.Attach(selected);
            selected.RegistrationNumber = "2";
            var vehicleDto = Mapper.Map <VehicleDto>(selected);

            controller.Put(vehicleDto);

            var result = vehiclesMockSet.Object.FirstOrDefault();

            Assert.Equal(result?.RegistrationNumber, "2");
        }
        public void RegisterPackage_AddsPackageToDatabase()
        {
            var mockSet     = MockDbSetHelper.CreateMockSet(new Package[] { }.AsQueryable());
            var mockContext = new Mock <InstantDeliveryContext>();

            mockContext.Setup(m => m.Packages).Returns(mockSet.Object);
            mockContext.Setup(m => m.PackageEvents).ReturnsDbSet(new List <PackageEvent>());
            var pricingMock = new Mock <IPricingStrategy>();
            var package     = new Package
            {
                Width  = 100,
                Height = 200,
                Length = 150,
                Weight = 100
            };

            var controller = new PackagesController(mockContext.Object, pricingMock.Object);
            var packageDto = new PackageDto();

            Mapper.DynamicMap(package, packageDto, typeof(Package), typeof(PackageDto));
            controller.RegisterPackage(packageDto);

            mockSet.Verify(m => m.Add(It.IsAny <Package>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public void RemovePackage_ShouldRemovePackage()
        {
            var package = new Package
            {
                Id     = 1,
                Status = PackageStatus.InDelivery
            };
            var packages = new List <Package> {
                package
            };
            var packagesMockSet = MockDbSetHelper.CreateMockSet(packages.AsQueryable());

            var employee = new Employee {
                Id = 1, FirstName = "A", LastName = "B"
            };

            var mockContext = new Mock <InstantDeliveryContext>();

            mockContext.Setup(c => c.Packages).Returns(packagesMockSet.Object);
            mockContext.Setup(c => c.Employees).ReturnsDbSet(employee);

            var controller = new PackagesController(mockContext.Object, null);

            controller.Delete(package.Id);

            packagesMockSet.Verify(m => m.Remove(It.Is((Package p) => p.Id == package.Id)), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public void RegisterPackage_ForValidatedPackage_ShouldAddNewPackage()
        {
            var package = new Package
            {
                Id     = 1,
                Height = 100,
                Weight = 100,
                Width  = 100,
                Length = 100,
                Status = PackageStatus.InWarehouse
            };
            var packages        = new List <Package>().AsQueryable();
            var packagesMockSet = MockDbSetHelper.CreateMockSet(packages);

            var mockContext = new Mock <InstantDeliveryContext>();

            mockContext.Setup(c => c.Packages).Returns(packagesMockSet.Object);
            mockContext.Setup(m => m.PackageEvents).ReturnsDbSet(new List <PackageEvent>());
            var pricingStrategy = new Mock <IPricingStrategy>().Object;
            var controller      = new PackagesController(mockContext.Object, pricingStrategy);
            var packageDto      = new PackageDto();

            Mapper.DynamicMap(package, packageDto, typeof(Package), typeof(PackageDto));
            controller.RegisterPackage(packageDto);

            packagesMockSet.Verify(m => m.Add(It.IsAny <Package>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public void RemoveVehicle_ShouldRemoveVehicle()
        {
            var vehicles = new List <Vehicle> {
                new Vehicle
                {
                    Id = 1,
                    RegistrationNumber = "1"
                }
            }.AsQueryable();
            var vehiclesMockSet = MockDbSetHelper.CreateMockSet(vehicles);
            var mockContext     = new Mock <InstantDeliveryContext>();

            mockContext.Setup(c => c.Vehicles).Returns(vehiclesMockSet.Object);

            var controller = new VehiclesController(mockContext.Object);

            controller.Delete(vehicles.First().Id);

            vehiclesMockSet.Verify(m => m.Remove(vehicles.First()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public void AddVehicle_ShouldAlwaysAddVehicle()
        {
            var vehicleModel = new VehicleModel {
                Id = 1
            };
            var vehicles        = new List <Vehicle>().AsQueryable();
            var vehiclesMockSet = MockDbSetHelper.CreateMockSet(vehicles);
            var vehicleToAdd    = new AddVehicleDto {
                Id = 1, VehicleModelId = 1, RegistrationNumber = ""
            };

            var mockContext = new Mock <InstantDeliveryContext>();

            mockContext.Setup(c => c.Vehicles).Returns(vehiclesMockSet.Object);
            mockContext.Setup(c => c.VehicleModels).ReturnsDbSet(vehicleModel);

            var controller = new VehiclesController(mockContext.Object);

            controller.Post(vehicleToAdd);

            vehiclesMockSet.Verify(m => m.Add(It.Is((Vehicle v) => v.Id == vehicleToAdd.Id)), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }