'How to test database methods and get code coverage xUnit ASP.NET
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).
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nick the Community Scientist |