How to moq Entity Framework SaveChangesAsync?

醉酒当歌 提交于 2019-12-04 03:19:01

You are right the problem occurs because one of your setups incorrect :: dbContext.Setup(c => c.SaveChangesAsync()).Verifiable();.

The method return a Task and you forgot to return a Task therefore null returns.

You can remove dbContext.Setup(c => c.SaveChangesAsync()).Verifiable(); or change the setup to something like:

dbContext.Setup(c => c.SaveChangesAsync()).Returns(() => Task.Run(() =>{})).Verifiable();

You could use

dataContext.Setup(x => x.SaveChangesAsync()).ReturnsAsync(1);

and

dataContext.Verify(x=>x.SaveChangesAsync());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!