如何测试数据库方法并获取代码覆盖xunit asp.net
我最初使用 moq 用于测试的库,因为我实际上不想弄乱数据库中的数据。但是,我很快意识到,而不是以某种方式运行代码并伪造结果,而是后者。由于几天内项目截止日期,我需要达到至少80%的代码覆盖范围,并且数据库是该代码的很大的chuck。 我在单独的类中有一些DB的方法,例如:
公共任务< bool> dbempty();
公共任务< bool> parsetodb();
公共任务< customer> getCustomer(int ID);
我想要这些方法之类的东西,所以我不会弄乱我的数据,还可以获取代码覆盖范围。除非习惯上使用标记数据库方法[排除fromCodecoverage]
是吗?我以前在单元测试的唯一经验是计算器应用程序,我对这项运动是全新的,这是我第一次使用AC#测试框架(XUNIT)。
I initially used the Moq library for testing, since I don't actually want to mess the data in my database. However, I quickly realized instead of running the code in some way and faking the result, it just does the latter. I need to reach a code coverage of at least 80% due to a project deadline in a couple of days, and the database is a big chuck of the code.
I have some methods for my DB in separate classes such as:
public Task<bool> DBEmpty();
public Task<bool> ParseToDB();
public Task<Customer> GetCustomer(int id);
I want these methods something like mocked, so I don't mess my data, but also get code coverage. Unless it is customary that database methods are traditionally marked with [ExcludeFromCodeCoverage]
, is it? My only previous experience with unit testing is a calculator app, I am brand new to this sport and this is the first time I use a c# testing framework (xUnit).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的情况的答案:我最终没有使用内存数据库,因为我的情况没有足够的时间。我使用 Moq 来伪造来自数据库/服务类的所有响应,并将它们排除在覆盖范围之外。
Answer in my case: I did not end up using an in memory database, since there was not enough time for this in my case. I used Moq to fake all responses from the database/service classes and excluded them from coverage.