samedi 10 septembre 2016

C# Entity Test - While debugging, test pass but in run mode fail

I want to check if my remove method works :) I added one tuple to my database.

TestMethod

 [TestMethod]
    public void DeleteIngredient()
    {
        using (var ctx = new TestDbContext())
        {
            //Arrange
            Ingredient ingredient = new TestClass.Ingredient();
            ingredient.Id = 1;
            ingredient.Name = "Watermelon";
            ingredient.VegeOrVegetarian = VeganType.Both;
            //Act
            ctx.ingredients.Add(ingredient);
            ctx.SaveChanges();
            IngredientService service = new IngredientService(ctx);
            service.removeIngredient(1);
            service.saveChanges();

            //Assert
            Assert.AreEqual(0, ctx.ingredients.Count());
        }
    }

Service

        public class IngredientService
{
    private readonly TestDbContext _TestDbContext;

    public IngredientService(TestDbContext TestDbContext)
    {
        _TestDbContext = TestDbContext;
    }

    public List<Ingredient> getIngredients()
    {
        return _TestDbContext.ingredients.ToList();
    }

    public void removeIngredient(int id)
    {
        var toRemove = _TestDbContext.ingredients.Find(id);       
        _TestDbContext.ingredients.Remove(toRemove);

    }
    public void saveChanges()
    {
        _TestDbContext.SaveChanges();
    }
}

Everthing works in debugging mode.

Assert.AreEqual(0, ctx.ingredients.Count());

i am informed about test pass. But after running all test in run mode including this delete i am getting error.

Assert.AreEqual failed Expected: <0>. Actual<1>

I think there is problem with context. Any clue how to solve this ?

Aucun commentaire:

Enregistrer un commentaire