How to add Mock db tables in C# test cases

1k Views Asked by At

How to create mock db tables for the separate class file in test cases to access the service test case and also I need for that tables between parent and child relation

 public static class MockTestData
    {
        // Test data for the DbSet<User> getter
        public static IQueryable<EaepTieriiLangComp> Langcomps
        {
            get
            {   return new List<EaepTieriiLangComp>
                {
                     new EaepTieriiLangComp{EaepAssessmentId=1,LangCompId=1,IsPrimary ="Y",LangId =1,LangReadId=1,LangWrittenId=1,LangSpokenId=1,LangUnderstandId=1 },
                     new EaepTieriiLangComp{EaepAssessmentId=2,LangCompId=1 ,IsPrimary ="N",LangId =2,LangReadId=2,LangWrittenId=2,LangSpokenId=2,LangUnderstandId=2 }//Lang =obj,LangRead=objRead,LangSpoken =objSpeak,LangWritten=objWrite,LangUnderstand=objUnderstand
                }.AsQueryable();
            }
        }
        public static IQueryable<LookupLang> LookupLangs
        {
            get
            {   return new List<LookupLang>
                {
                   new LookupLang{LangId = 1,Description = "lang1",IsActive="Y"},
                   new LookupLang{LangId = 2,Description = "lang2",IsActive="N"}
                }.AsQueryable();
            }
        }
}`

enter code here` I tried for the above flow but i didnot get relatons for that tables

2

There are 2 best solutions below

0
Madhu On BEST ANSWER

Thank you so much advise to use EF core.InMemory package it is working fine now I followed below code

Inmemory class

using Assessments.TierIIQueryDataModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace AssessmentCommandTest.Helpers
{
    public class InMemoryDataProviderQueryService : IDisposable
    {

       
        private bool disposedValue = false; // To detect redundant calls

        public DbQueryContext CreateContextForInMemory()
        {
            var option = new DbContextOptionsBuilder<DbQueryContext>().UseInMemoryDatabase(databaseName: "Test_QueryDatabase").Options;

            var context = new DbQueryContext(option);
            if (context != null)
            {
                //context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }
            return context;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                }

                disposedValue = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
      
    }

}


and access to DbQueryContext conext file in my code and write mock tables as below

using AssessmentCommandTest.Helpers;
using Assessments.TierIIQueryDataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AssessmentCommandTest.MockDbTables
{
    public class MockQueryDbContext
    {
        public TierIIQueryContext MockTierIIQueryContexts()
        {
           

//Create object for Inmemory DB provider var factory = new InMemoryDataProviderQueryService();

        //Get the instance of TierIIQueryContext
        var context = factory.CreateContextForInMemory();

        context.LookupLang.Add(new LookupLang { LangId = 1, Description = "Arabic", IsActive = "Y" });
        context.LookupLang.Add(new LookupLang { LangId = 2, Description = "Bangali", IsActive = "Y" });
        context.LookupLang.Add(new LookupLang { LangId = 3, Description = "English", IsActive = "Y" });
        context.LookupLang.Add(new LookupLang { LangId = 4, Description = "French", IsActive = "Y" });
enter code here

context.SaveChanges();
            return context;
        }
    }
} 
1
lobstar On

If you are using EF Core, you can create inmemory database, add data and make query to it. Here is example:

First you need install Microsoft.EntityFrameworkCore.InMemory package. After this make options:

_options = new DbContextOptionsBuilder<SomeDbContext>()
            .UseInMemoryDatabase(databaseName: "DbTest")
            .Options;
using var context = new SomeDbContext(_options);
context.Database.EnsureCreated();

Then add your data:

 context.AddRange(
   new LookupLang{LangId = 1,Description = "lang1",IsActive="Y"},
   new LookupLang{LangId = 2,Description = "lang2",IsActive="N"}
)

And now you can use context for testing purposes