コード例 #1
0
        public void DeleteRecipe_Deletes_Existing_Recipe()
        {
            var recipe1 = new Recipe()
            {
                Description = "Test description 1",
                Id          = 1,
                Name        = "Test Recipe Name 1",
                UserId      = "user1"
            };
            var recipe2 = new Recipe()
            {
                Description = "Test description 2",
                Id          = 2,
                Name        = "Test Recipe Name 2",
                UserId      = "user2"
            };

            var dbOptions = new DbContextOptionsBuilder <ApplicationDbContext>()
                            .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                            .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
                            .Options;

            var someOptions = Options.Create(new OperationalStoreOptions());

            using (var context = new ApplicationDbContext(dbOptions, someOptions))
            {
                context.Recipes.Add(recipe1);
                context.Recipes.Add(recipe2);
                context.SaveChanges();

                var recipesRepository = new RecipesRepository(context);
                recipesRepository.DeleteRecipe(recipe2);
            };

            using (var context = new ApplicationDbContext(dbOptions, someOptions))
            {
                Assert.AreEqual(1, context.Recipes.Count());
                Assert.IsTrue(context.Recipes.Contains(recipe1));
            }
        }