コード例 #1
0
        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());
        }
コード例 #2
0
        public void GetAllAvailable_ForSpecifiedVehicle()
        {
            var vehicleModel = new VehicleModel { Id = 1 };
            var vehicles = new List<Vehicle>
            {
                new Vehicle {Id = 1, RegistrationNumber = "1", VehicleModel = vehicleModel},
                new Vehicle {Id = 2, RegistrationNumber = "2", VehicleModel = vehicleModel},
                new Vehicle {Id = 3, RegistrationNumber = "3", VehicleModel = vehicleModel},
            };
            var employees = new List<Employee>
            {
                new Employee {FirstName = "J.D", LastName = "Kyle", Vehicle = vehicles[1]},
                new Employee {FirstName = "Ted", LastName = "Mosby", Vehicle = vehicles[0]},
            };
            var mockContext = new Mock<InstantDeliveryContext>();
            mockContext.Setup(c => c.Employees).ReturnsDbSet(employees);
            mockContext.Setup(c => c.Vehicles).ReturnsDbSet(vehicles);
            mockContext.Setup(c => c.VehicleModels).ReturnsDbSet(vehicleModel);

            var controller = new VehiclesController(mockContext.Object);

            var result = controller.GetAllAvailable(new PageQuery { PageSize = 10, PageIndex = 1 })
                as OkNegotiatedContentResult<PagedResult<VehicleDto>>;

            Assert.Equal(result?.Content.PageCollection.Count, 1);
        }