public async void GetSingleDeliveryTest() { var delivery = new Delivery { OrderId = 10, Status = DeliveryStatus.Approved, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, Sender = new Partner { Name = "Ikea" }, Recipient = new User { FirstName = "John", LastName = "Doe", Email = "*****@*****.**", Address = "Test Street, London", Phone = "0845345" } }; dbContext.Add(delivery); dbContext.SaveChanges(); var controller = new DeliveryController(deliveryService, mapper);; controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new System.Security.Claims.ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "1") })) } }; var response1 = await controller.Get(delivery.Id); var okResult1 = response1 as OkObjectResult; Assert.NotNull(okResult1); Assert.Equal(200, okResult1.StatusCode); Assert.NotNull(okResult1.Value); var delivery1 = okResult1.Value as DeliveryViewModel; Assert.Equal(delivery.Id, delivery1.Id); }
public DeliveryControllerTests() { var config = new MapperConfiguration( cfg => { cfg.AddProfile <TestMapperProfile>(); }); mapper = config.CreateMapper(); var options = new DbContextOptionsBuilder <DeliverItContext>() .UseInMemoryDatabase(databaseName: "Add_writes_to_database") .Options; if (File.Exists("deliveryittest.db")) { File.Delete("deliveryittest.db"); } var sqliteOptions = new DbContextOptionsBuilder <DeliverItContext>().UseSqlite("Data Source=deliveryittest.db", builder => { }).Options; mapper = config.CreateMapper(); dbContext = new DeliverItContext(options); dbSqlite = new DeliverItContext(sqliteOptions); dbSqlite.Database.EnsureDeleted(); dbSqlite.Database.Migrate(); deliveryService = new DeliveryService(dbContext); dbContext.Add <Delivery>(new Delivery { OrderId = 1, Status = DeliveryStatus.Created, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, Sender = new Partner { Name = "Ikea" }, Recipient = new User { FirstName = "John", LastName = "Doe", Email = "*****@*****.**", Address = "Test Street, London", Phone = "0845345" } }); dbContext.Add <Delivery>(new Delivery { OrderId = 10, Status = DeliveryStatus.Approved, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, }); dbContext.Add <Delivery>(new Delivery { OrderId = 11, Status = DeliveryStatus.Approved, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, }); dbContext.Add <Delivery>(new Delivery { OrderId = 13, Status = DeliveryStatus.Approved, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, }); dbContext.Add <Delivery>(new Delivery { OrderId = 9, Status = DeliveryStatus.Completed, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, }); dbContext.Add <Delivery>(new Delivery { OrderId = 10, Status = DeliveryStatus.Expired, AccessWindow = new AccessWindow { StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(2) }, }); dbContext.SaveChanges(); }