0

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

2
  • I wouldn't call it solving the problem. You are downloading all the items and perform the filtering on the client-side which will influence your application performance. Anyway could you please share the code that you used in your test to configure the mock? Commented Apr 18, 2012 at 18:48
  • How is the data being mocked up? Are you setting the ID correctly? Like dmusial said, code would be helpful. Commented Apr 18, 2012 at 20:02

1 Answer 1

1

You get null because you never setup your CountyService to return proper data when calling GetById. Add the below code for setting up the mock and you'll be good to go with the GetById method:

mock.Setup(m => m.GetById(1).Returns(new State { Id = 1, ... });

or set it up to work with any argument passed to the mothod:

mock.Setup(m => m.GetById(It.IsAny<int>()).Returns(new State { ... });
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, that did it. Just getting into the unit testing thing as it is important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.