I started using JustMock to mock my Linq To SQL (L2S) based repository. Everything works well for my Get operations; however, I am getting a consistent error when I attempt to mock for Insert operations. The following is my code:
The call to the Add method in the Customer repository is as follows:
The system generates a NullReferenceException error on the InsertOnSubmit call and never gets to the SubmitChanges line.
I moved the same code to a console app for testing and it executed correctly (minus the JustMock embellishments of course). What is the correct way to utilize JustMock to get the above code to test properly.
var context = Mock.Create<
MyDataContext
>();
var repository = new CustomerRepository(context);
var customer = new Customer
{
CustomerId = 0,
CompanyId = 1,
CustomerNo = "FAKE4",
FullName = "Isaac Fake4",
LastName = "Fake",
FirstName = "Isaac",
Sex = "M",
Birthdate = new DateTime(2000, 12, 1),
IsRecordActive = true,
IsRecordDeleted = false,
ModifiedOn = DateTime.Now,
ModifiedBy = "System.User"
};
Mock.Arrange(context.SubmitChanges).DoInstead(() =>
{ customer.CustomerId = 5; });
//Act
var actual = repository.Add(customer);
//Assert
Assert.AreEqual(5, actual.CustomerId);
The call to the Add method in the Customer repository is as follows:
public Customer Add(Customer customer)
{
this.context.Customer.InsertOnSubmit(customer);
this.context.SubmitChanges();
return customer;
}
The system generates a NullReferenceException error on the InsertOnSubmit call and never gets to the SubmitChanges line.
I moved the same code to a console app for testing and it executed correctly (minus the JustMock embellishments of course). What is the correct way to utilize JustMock to get the above code to test properly.