如何在UTest类中初始化内存数据库上下文

发布于 2025-01-11 18:27:48 字数 1749 浏览 0 评论 0原文

如何在 UnitTest 类中初始化假内存数据库上下文以使用 CatDbContext 来测试存储库。我收到错误:OneTimeSetUp:找不到合适的构造函数。 我需要如何初始化数据库,以用于测试catRepository。

    namespace CatsCRUDAppTest
{
    public class CatRepositoryTests
    {
        private IRepository<Cat> catRepository;
        private FakeDbContext context;

        private Cat defaultCat = new Cat() {Id = 0, Name = "name" };

        // OneTimeSetUp: No suitable constructor was found
        public CatRepositoryTests(FakeDbContext fake)
        {
            catRepository = new CatRepository(fake);
            context = fake;
        }
        //

        [Test]
        public void CreateTrueTest()
        {
            Cat cat = defaultCat;
            catRepository.Create(cat);
            context.SaveChanges();

            Cat existingCat = context.Find<Cat>(cat.Id);

            Assert.IsNotNull(existingCat);
        }
    }

存储库类: 该类获取 CatDbContext 进行初始化。

    namespace DAL.Repositories
{
    public class CatRepository : IRepository<Cat>
    {
        private CatDbContext db;

        public CatRepository(CatDbContext context)
        {
            db = context;
        }
        public async void Create(Cat item)
        {
            await db.Cats.AddAsync(item);
        }

假数据库上下文:

public class FakeDbContext : CatDbContext
    {
        public FakeDbContext(DbContextOptions<CatDbContext> options) : base(options)
        {
        }

        public DbSet<Cat> Cats { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase(databaseName: "FakePersonalSiteDbContext");
        }
}

How to initialize fake inmemory database context in UnitTest class for testing repository, which using CatDbContext. I got Error: OneTimeSetUp: No suitable constructor was found.
How i need ti initialize database, to use for testing catRepository.

    namespace CatsCRUDAppTest
{
    public class CatRepositoryTests
    {
        private IRepository<Cat> catRepository;
        private FakeDbContext context;

        private Cat defaultCat = new Cat() {Id = 0, Name = "name" };

        // OneTimeSetUp: No suitable constructor was found
        public CatRepositoryTests(FakeDbContext fake)
        {
            catRepository = new CatRepository(fake);
            context = fake;
        }
        //

        [Test]
        public void CreateTrueTest()
        {
            Cat cat = defaultCat;
            catRepository.Create(cat);
            context.SaveChanges();

            Cat existingCat = context.Find<Cat>(cat.Id);

            Assert.IsNotNull(existingCat);
        }
    }

Repository class:
That class get CatDbContext to initialize.

    namespace DAL.Repositories
{
    public class CatRepository : IRepository<Cat>
    {
        private CatDbContext db;

        public CatRepository(CatDbContext context)
        {
            db = context;
        }
        public async void Create(Cat item)
        {
            await db.Cats.AddAsync(item);
        }

FakeDbContext:

public class FakeDbContext : CatDbContext
    {
        public FakeDbContext(DbContextOptions<CatDbContext> options) : base(options)
        {
        }

        public DbSet<Cat> Cats { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase(databaseName: "FakePersonalSiteDbContext");
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2025-01-18 18:27:48

需要使用dbBuilder。

 private IRepository<Cat>? catRepository;
            private FakeDbContext? context;
    
        [SetUp]
        public void SetUp()
        {
            var dbBuilder = new DbContextOptionsBuilder<CatDbContext>();
            dbBuilder.UseInMemoryDatabase(databaseName: "FakeDbContext");
            context = new (dbBuilder.Options);
            catRepository = new CatRepository(context);
        }

Need to use dbBuilder.

 private IRepository<Cat>? catRepository;
            private FakeDbContext? context;
    
        [SetUp]
        public void SetUp()
        {
            var dbBuilder = new DbContextOptionsBuilder<CatDbContext>();
            dbBuilder.UseInMemoryDatabase(databaseName: "FakeDbContext");
            context = new (dbBuilder.Options);
            catRepository = new CatRepository(context);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文