public async static Task <MockDbContext> GetDbContext(bool withData = true) { var options = new DbContextOptionsBuilder().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; var db = new MockDbContext(options); if (withData) { var basket1 = new Basket.API.Models.Basket { Id = 1, State = API.Enums.BasketStates.PENDING, CreatedAt = DateTime.Now, User = 1 }; var basket2 = new Basket.API.Models.Basket { Id = 2, State = API.Enums.BasketStates.PENDING, CreatedAt = DateTime.Now, User = 2 }; var basket3 = new Basket.API.Models.Basket { Id = 3, State = API.Enums.BasketStates.PENDING, CreatedAt = DateTime.Now, User = 3 }; db.Baskets.Add(basket1); db.Baskets.Add(basket2); db.Baskets.Add(basket3); db.Bookings.Add(new Booking { Id = 1, BasketID = 2, Price = 90, ProductID = 1 }); db.Bookings.Add(new Booking { Id = 2, BasketID = 2, Price = 40, ProductID = 2 }); db.Bookings.Add(new Booking { Id = 3, BasketID = 1, Price = 90, ProductID = 3 }); await db.SaveChangesAsync(); } return(db); }
public async Task Should_Not_Create_Basket_If_It_Does_Exists() { using (var context = await MockDbContext.GetDbContext()) { controller = new BasketsController(context); var basketMock = new Basket.API.Models.Basket { User = 3 }; var actionResult = await controller.PostItemAsync(basketMock) as ObjectResult; Assert.Equal((int)HttpStatusCode.BadRequest, (actionResult as ObjectResult).StatusCode); } }
public async Task Should_Create_Basket_If_It_Does_Not_Exists() { using (var context = await MockDbContext.GetDbContext()) { controller = new BasketsController(context); var basketMock = new Basket.API.Models.Basket { User = 10 }; var actionResult = await controller.PostItemAsync(basketMock); var returnedObject = (actionResult as ObjectResult).Value as Basket.API.Models.Basket; Assert.Equal(201, (actionResult as ObjectResult).StatusCode); Assert.Equal(4, returnedObject.Id); Assert.Equal(10, returnedObject.User); Assert.Equal(BasketStates.PENDING, returnedObject.State); } }