1

So far I have this :

 using (var actContext = new ItsAllAboutTheGameDbContext(contextOptions))
        {
            var cardService = new CardService(actContext);
            creditCardResult = await cardService.AddCard("3242423532532434", "332", DateTime.Parse("02.03.2020"), user);
            actContext.CreditCards.Add(creditCardResult);
            await actContext.SaveChangesAsync();


        }

        //Assert

After I have added the card to CreditCards table in the context, how can I Assert if it was added successfully? And I need to check if it is added into the inmemorydatabase (actContext)?

1 Answer 1

1

Do you really want to assert that the data was inserted in the table?

You should inject a DBContext factory into your controller and use a fake one on your unit tests.

public class MyController : Controller { public MyController(Func dbContextFactory) ...

...

    using (var actContext = dbContextFactory())

}

Then, in your test, you just inject a factory to mock context.

Better yet, inject a component in your controller.

Otherwise, you're left with querying the database to se if the data is there.

1
  • 1
    I have heard that it is somewhat in many cases useless to test the DBContext, because it is equal to testing if C# compiler works correctly. Dec 15, 2018 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.