0

如何为测试用例中的单独类文件创建模拟数据库表以访问服务测试用例,并且我还需要父子关系之间的表

 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();
            }
        }
}`

在此处输入代码`我尝试了上述流程,但我没有获得该表的关系

4

2 回答 2

1

如果您使用的是 EF Core,您可以创建内存数据库、添加数据并对其进行查询。这是示例:

首先你需要安装Microsoft.EntityFrameworkCore.InMemory包。在此之后制作选项:

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

然后添加您的数据:

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

现在您可以使用上下文进行测试

于 2020-09-08T19:47:40.380 回答
0

非常感谢您建议使用 EF core.InMemory 包它现在工作正常我按照下面的代码

记忆类

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()
        {
           

//为内存数据库提供者创建对象 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;
        }
    }
} 
于 2020-09-10T11:17:18.453 回答