【发布时间】:2018-05-13 05:26:26
【问题描述】:
我尝试测试我的应用,因此我需要模拟我的 EF 上下文。
我的代码似乎没问题,但我有以下异常:
"System.ArgumentNullException : 值不能为空。参数名称: 来源”
这是我的测试方法:
var options = new DbContextOptionsBuilder<ProductContext>().Options;
var settings = new SqlSettings
{
InMemory = true
};
var context = new Mock<ProductContext>(options, settings);
var mockTreeService = new TreeService(context.Object);
await mockTreeService.CreateTreeAsync("Testing tree", Guid.NewGuid());
context.Verify(x => x.AddAsync(It.IsAny<Tree>(), CancellationToken.None), Times.Once);
貌似是在执行这段代码的时候抛出了这个异常
var tree = await _context.Trees
.Include(x => x.Translation)
.FirstOrDefaultAsync(x => x.Translation.Pl == name);
它来自我正在测试的服务
【问题讨论】:
-
在我看来“包含”会产生问题,但我不知道如何解决。
-
您可能会发现EF Core Testing 文档很有用。
-
您不需要在 EF 核心中模拟 ProductContext,而是使用 InMemory ProductContext
标签: c# entity-framework asp.net-core entity-framework-core