jeudi 7 février 2019

.net core mocking dbcontext not working (static issue?)

I am trying to test .net core 2.2 api's. I am having difficulty mocking (using moq) the dbcontext. What is the syntax for adapting my mockDbContext to be usable. I receive a NullReferenceException. Since the Changetracker is never instantiated I believe. Do I need a different approach? I saw mentioned .UseInMemoryDatabase() but, with very little documentation or good examples.

Below is the code I am trying to use in my test [Fact].

     var mockDbContext = new Mock<dbContext>(optionsBuilder.Options);
     var controller = new HomeController(mockDbContext.object);

Then use controller to test... removed for brevity

var datafromdbcontext = controller.GetData();

Below is an example of my dbcontext.

    public class dbContext:DbContext
    {
        public dbContext(DbContextOptions<dbContext> options)
        : base(options)
        {
            //MAKE IT READONLY
            ChangeTracker.QueryTrackingBehavior = 
       QueryTrackingBehavior.NoTracking;
        }

Aucun commentaire:

Enregistrer un commentaire