如何在测试中初始化IServiceScopeFactory以使用Entity Framework核心InMemory数据库

问题描述

我有一个包括Crud函数的HotelRepository类,我想使用内存数据库中的EF核心测试我的所有存储库,但由于我的存储库类构造函数使用IServiceScopeFactory,我被卡住了

Public class HotelRepository
{
    IScopedServiceFactory factory
    public HotelRepository(IScopedServiceFactory factory)
    {
        this.factory = factory;
    }
    public CreateHotel(HotelInformation)
    {
        var context = Create();
        //code to implement save
        context.savechanges();
    }
    
    private HotelDbContext Create()
    {
        return factory.CreateScope().ServiceProvidor.GetRequiredService<Dbcontext>();
    }
}

//using ms tests
public class HotelDb_Tests
{
    public Test_Save_Hotels()
    {
        var options = new DbCOntextOptionsBuilder<DbContext>().UseInMemoryDatabase("TestHotelDb").Options;
        using (var context = new DbContext(Options))
        {
            //How to create instance of HotelRepository which takes IScopedServiceFactory as parameter 
            //Basically want to do the following
            var HotelRepo = new HotelRepo(ScopedServiceFactory);
            HotelRepo.CreateHotel(HotelInformation)
        }
    }
    
}

我如何初始化HotelRepository实例而不是使用内存数据库来测试不同的功能

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)