I am newbie to both Entity Framework
and Moq testing
.
Below is my EF code
:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
Context = context;
}
public TEntity Get(int id)
{
return Context.Set<TEntity>().Find(id);
}
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
public void Add(TEntity entity)
{
Context.Set<TEntity>().Add(entity);
}
public void Remove(TEntity entity)
{
Context.Set<TEntity>().Remove(entity);
}
}
public interface IRepository<TEntity> where TEntity : class
{
TEntity Get(int id);
IEnumerable<TEntity> GetAll();
void Add(TEntity entity);
void Remove(TEntity entity);
}
public interface IUnitOfWork : IDisposable
{
int Complete();
}
public class UnitOfWork : IUnitOfWork
{
private readonly EF_SalesOrdersDBContext _context;
public CustomersRepository customersRepository { get; set; }
public UnitOfWork(EF_SalesOrdersDBContext context)
{
_context = context;
customersRepository = new CustomersRepository(_context);
}
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
public class CustomersRepository : Repository<Customer>
{
public CustomersRepository(EF_SalesOrdersDBContext context)
:base(context)
{
}
}
Code below in program.cs
which works.
UnitOfWork unitOfWork = new UnitOfWork(new EF_SalesOrdersDBContext());
unitOfWork.customersRepository.Add(new Customer { CompanyName = "test2", FirstName = "John", LastName = "Barnes", AddressLine1 = "11 lfc", City = "Liverpool", County = "LFC", Phone = "444" });
unitOfWork.Complete();
I would like test using Moq
.
This is what I have tried:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mockSet = new Mock<DbSet<Customer>>();
var mockContext = new Mock<EF_SalesOrdersDBContext>();
mockContext.Setup(m => m.Customers).Returns(mockSet.Object);
var service = new CustomersRepository(mockContext.Object);
service.Add(new Customer { CompanyName = "test2", FirstName = "John", LastName = "Barnes", AddressLine1 = "11 lfc", City = "Liverpool", County = "LFC", Phone = "444" });
mockSet.Verify(m => m.Add(It.IsAny<Customer>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
}
}
I get the error:
Message: Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: System.NullReferenceException: Object reference not set to an instance of an object. Stack Trace: Repository`1.Add(TEntity entity) line 34 UnitTest1.TestMethod1() line 26
Mocking db context is a bad idea - you can simply use an in memory context and avoid mocking everything. It will work just like the real database.
See these suggestions from Microsoft: https://docs.microsoft.com/en-us/ef/core/testing/
This question has some answers that show how to make the in memory context: Unit testing with EF Core and in memory database
A key reason for implementing a repository pattern with Entity Framework is to enable unit testing. The Repository becomes the abstraction point for the unit tests. Basically think of it this way:
So where your main business logic should be residing in your Services / Controllers, that is where your unit tests would be targeting. The Mocks are made for your Repositories, not EF DbContexts/DbSets.
So for example, given your structure I would have a Mock of the UnitOfWork which asserts whether any Complete()
call is made (or not), then mock out the Repository for expected methods to return a known state.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.