[英]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.