I solved my problem, but am not sure why things are working and am asking because I always want to know why things work the way they do.
Ok, I am developing an MVC 3 application am utilizing a generic repository, unity for DI, and moq for unit testing. In my unit test for editing an item, my controller initially had this as follows: AnEntity obj = _anEntityService.GetById(id);
however, when my unit test was calling the Edit(1) in the controller, null was being returned. When I changed the read to be AnEntity obj = _anEntityService.GetAll().FirstOrDefault(p => p.Id == id);
it returned the mocked item correctly.
Now, I am glad I figured it out, however, I am still scratching my head as to why that worked for unit testing, but the GetById was working as I was running the application.
Any insight would be most helpfull.
Thanks in advance.
Here is the mocked data:
public class AdministrationMockData
{
#region Administration Mock Data
#region County Mock Data
public static void CreateCounty(ref Mock<ICountyService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<County>
{
new County { Id = 1, Name = "Adams", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 2, Name = "Berks", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 3, Name = "Chester", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 4, Name = "York", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 5, Name = "Baltimore", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 6, Name = "Montgomery", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion County Mock Data
#region State Mock Data
public static void CreateState(ref Mock<IStateService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<State>
{
new State { Id = 21, Name = "Maryland", Code = "MD", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new State { Id = 39, Name = "Pennsylvania", Code = "PA", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion State Mock Data
#endregion Administration Mock Data
}
Here is the Unit Test for Edit
[TestMethod]
public void Can_Edit_County()
{
// Arrange
// - create the mock repositories
Mock<ICountyService> mockCounty = new Mock<ICountyService>();
Mock<IStateService> mockState = new Mock<IStateService>();
AdministrationMockData.CreateCounty(ref mockCounty);
AdministrationMockData.CreateState(ref mockState);
// Arrange
// - create a controller and make the page size 6 items
CountyController controller = new CountyController(mockCounty.Object, mockState.Object);
controller.PageSize = 6;
controller.Testing = true;
// Act
County c1 = controller.Edit(1).ViewData.Model as County;
County c2 = controller.Edit(2).ViewData.Model as County;
County c3 = controller.Edit(3).ViewData.Model as County;
// Assert
Assert.AreEqual(1, c1.Id);
Assert.AreEqual(2, c2.Id);
Assert.AreEqual(3, c3.Id);
}
Here is the controller edit routine:
public ViewResult Edit(int id)
{
//County obj = _countyService.GetById(id);
County obj = _countyService.GetAll().FirstOrDefault(p => p.Id == id);
if (!Testing)
{
PopulateCountyDropDownLists(obj.StateId);
}
return View(obj);
}
I have a generic repository and interface repository which is instantiated by TEntity for each specific entity and also have a service and interface service to execute the repositories. My repository and service is based on: http://efmvc.codeplex.com/releases/view/58663