I have just started out using Moq for unit testing have have run into an issue when trying to add an object to our repo. Basically, we have an interface which represents the structure of our DB. This interface contains additional interfaces that represent the data within that DB, like this:
public interface IUnitOfWork : IDisposable
{
IRepository<Order> OrdersRepo { get; }
IRepository<Customer> CustomerRepo { get; }
IRepository<Product> ProductsRepo { get; }
}
Creating the mock IUnitOfWork is no problem, the issue arises when I attempt to add an order object to the OrdersRepo, like this:
[TestClass]
public class OrderTest
{
private Mock<IUnitOfWork> mockDB = new Mock<IUnitOfWork>();
private IUnitOfWork testDB;
private Order testOrder;
[TestInitialize]
public void Initialize()
{
//Create the test order
testOrder = new Order();
testOrder.ID = 123;
//Setting up the Moq DB
testDB = mockDB.Object;
}
[TestMethod]
public void AddOrder_ValidOrder_OrderAdded()
{
testDB.OrdersRepo.Add(testOrder);
}
}
I keep getting a NullReferenceException when I attempt to add the order. I think this is because the OrdersRepo inside the testDB is an interface. However when I tried to create a mock repo for this I get an error saying that the OrdersRepo is readonly because it is { get; } and not { get; set; }.
Is it possible for me to use Moq to test adding my order object when the repo is only get; and is an interface?
Aucun commentaire:
Enregistrer un commentaire