简体   繁体   English

使用带有接口的存储库模式时,如何使用实体框架和 Moq 进行单元测试?

[英]How I unit test with Entity Framework and Moq when using a repository pattern with Interface?

I am new to testing and have been tasked at work to setup some simple CRUD test on a MVC EF Core Repository for some code that I wrote using MSTest.我是测试新手,我的任务是在 MVC EF Core 存储库上为我使用 MSTest 编写的一些代码设置一些简单的 CRUD 测试。

One of get the methods eager loads a list using include, other wise it basic CRUD. get 方法之一是使用 include 加载列表,否则它是基本的 CRUD。

However I keep running into a discussion about either using in-memory db or mocking the ef core db and dbsets?但是,我一直在讨论使用内存数据库或 mocking ef core db 和 dbsets? What would be the best way to test an MVC EF Core Repository pattern with CRUD operations in a unit test?在单元测试中使用 CRUD 操作测试 MVC EF Core 存储库模式的最佳方法是什么?

 public DbSet<ResourceTags>? ResourceTags { get; set; }

 public class ResourceTags
 {

    [ForeignKey(nameof(ResourceRequest))]
    [Key]
    [Column(Order = 0)]
    public byte[] ResourceId { get; set; }

    [Key]
    [Column(Order = 1)]
    public string Tag { get; set; }

    public ResourceRequest ResourceRequest { get; set; }
}

   public ResourceTags? AddResourceTags(ResourceTags resourceTag)
   {
        try
        {
            var entity = this.prismContext.ResourceTags;

            if (entity != null)
            {
                var response = entity.Add(resourceTag);
                this.prismContext.SaveChanges();
                return response.Entity;
            }
        }
        catch
        {
            throw new Exception();
        }

        return null;
    }

    [TestMethod]
    public void AddResourceTagsSavesAResourceTagsViaContext()
    {
        Random rnd = new Random();
        var id = new byte[32];
        rnd.NextBytes(id);
        var text = "tagtext";
        string convertedId = Convert.ToBase64String(id);

        var mockResourceTag = new Mock<DbSet<ResourceTags>>();
        var mockPrismContext = new Mock<PrismContext>();

        mockPrismContext.Setup(m => m.ResourceTags).Returns(mockResourceTag.Object);

        var azureRepository = new AzureRepository(mockPrismContext.Object);
        var tag = new ResourceTags()
        {
            ResourceId = id,
            Tag = text,
        };

        var response = azureRepository.AddResourceTags(tag);
        Assert.IsNotNull(response);
    }

If you only use simple CRUD queries, and your queries do not depend on specific DB functions, eg, EF.Functions , then you should use InMemory Database , or SQLite with in-memory mode, which has better compatibility with real DBs.如果您只使用简单的 CRUD 查询,并且您的查询不依赖于特定的 DB 函数,例如EF.Functions ,那么您应该使用InMemory Database ,或者使用内存模式的SQLite ,它与真实 DB 的兼容性更好。

Mocking the DbSet will result in poor testing because your test may pass but when EF tries to translate your LINQ query to SQL at runtime, it may throw an exception, and you want to ensure that your translations work. Mocking DbSet将导致测试不佳,因为您的测试可能会通过,但是当 EF 尝试将您的 LINQ 查询转换为 SQL 时,它可能会在运行时引发异常,并确保您的翻译工作会抛出异常。

A third option would be to test against a docker container, ie, if you use SQL Server, you test against an SQL Server instance running in docker. A third option would be to test against a docker container, ie, if you use SQL Server, you test against an SQL Server instance running in docker. This way you remove any compatibility issues between DBs.这样您就可以消除 DB 之间的任何兼容性问题。

Sources:资料来源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Moq 对实体框架通用存储库进行单元测试 - Unit test an Entity Framework generic repository using Moq 如何使用MOQ和存储库模式对MVP中的事件进行单元测试? - How to unit test an event in MVP using MOQ and the repository pattern? 单元测试使用Moq的通用工作单元和存储库模式框架 - Unit Testing Generic Unit of Work and Repository Pattern framework using Moq 使用Moq进行单元测试的工作单元和通用存储库模式框架 - Unit Testing Unit of Work and Generic Repository Pattern framework using Moq 如何使用Lambda表达式进行单元测试实体框架/存储库模式 - How to unit test with lambda expressions Entity Framework/Repository pattern 如何对使用Entity Framework的存储库模式进行单元测试? - How to unit test a repository pattern that uses Entity Framework? 如何正确地对与Entity Framework / DbContext耦合的存储库模式方法进行单元测试? - How do I properly unit test a repository-pattern method, that has coupling to Entity Framework / DbContext? 在使用监督控制器模式时,我应该如何使用Moq视图对我的WebFormMVP演示者进行单元测试 - How should I unit test my WebFormMVP Presenters with Moq'd Views when using the Supervising Controller Pattern 我应该如何对使用Entity Framework实现的存储库方法进行单元测试? - How should I unit test a repository method implemented with Entity Framework? 如何使用Moq对实体框架6中的删除操作进行单元测试 - How to use Moq to unit test a delete operation in entity framework 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM