我试图测试我的应用程序,所以我需要模拟我的EF上下文。
我的代码似乎没问题,但是我遇到以下异常:
"System.ArgumentNullException:无法将空值分配给对象。参数名:source"
这是我的测试方法:
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);
这来自于我正在测试的服务