Although I have experience with TDD in another language, I am having a difficult time understanding testing in .NET Core 3. I am using Entity Framework, and trying to produce tests on my service layer. I have seen a few examples with repositories and some based on API, but I haven't figured out what I'm doing wrong.
I'm getting a System.ArgumentNullException : Value cannot be null. (Parameter 'source') error when running the test.
ICustomerService.cs
public interface ICustomerService
{
List<Customer> GetCustomers();
}
CustomerService.cs
public class CustomerService : ICustomerService
{
private readonly DbContext _db;
public CustomerService(DbContext db)
{
_db = db;
}
public List<Customer> GetCustomers()
{
return _db.Customer.ToList();
}
}
CustomerServiceTests.cs
[Fact]
public void GetCustomersReturnsRecords()
{
// Arrange
var cntxt = new Mock<DbContext>();
CustomerService cs = new CustomerService(cntxt.Object);
// Act
var result = cs.GetCustomers();
//Asserts here
Assert.NotNull(result);
}