Tuesday, September 20, 2011

deeper dive into our mocking

Trying to better understand how we mock...

using System;

using System.Collections.Generic;

using System.Linq;

using MyApp.Core.Infrastructure;

using MyApp.Core.Services;

using MyApp.Tests.Support;

using MyApp.Web.UI.Models;

using MyApp.Web.UI.Tests.Support;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MyApp.Core.Repositories;

using MyApp.Core.Entities;

using MyApp.Core;

using Moq;

using MyApp.Tests;

using MyApp.Web.UI.Controllers;

using System.Security.Principal;

using System.Web.Mvc;

using LinqSpecs;

namespace MyApp.Web.UI.Tests

{

   [TestClass]

   public class FooControllerTest : WebTestBase

   {

      MocksRegistry registry;

      private static string userName = WindowsIdentity.GetCurrent().Name;

      

      [TestInitialize]

      public void TestSetup()

      {

         registry = new MocksRegistry();

         ServiceLocator.LookupSource = () =>

         {

            return registry;

         };

      }

      

      [TestMethod]

      public void Can_Find_Within_Filtered_Set()

      {

         
14 lines of code go here

      }

      

      private IQueryable<Foo> BuildFoos()

      {

         var Bar = TestObjects.BuildFoo("Bar");

         Bar.Id = Guid.NewGuid();

         Bar.IsSane == false;

         var Baz = TestObjects.BuildFoo("Baz");

         Baz.Id = Guid.NewGuid();

         Baz.IsSane == true;

         List<Foo> foos = new List<Foo> { Bar, Baz };

         return foos.AsQueryable();

      }

   }

}

 
 

The 14 lines of code that are really interesting are:

  1. IEnumerable<Foo> foos = BuildFoos();
  2. Assert.AreEqual(2, foos.Count());
  3. var url = "/foo/index";
  4. var fooController = TestUtils.BuildTestController<FooController>("POST", url);
  5. fooController.Session["PrevUrl"] = url;
  6. fooController.Session["CurUrl"] = url;
  7. fooController.FooRepository = ServiceLocator.Get<IFooRepository>(AppCtxIds.FOO_REPOSITORY);
  8. registry.FooRepositoryMock.Setup(repo => repo.GetAll()).Returns(BuildFoos()).Verifiable();
  9. registry.FooRepositoryMock.Setup(repo => repo.FindAll(It.IsAny<Specification<Foo>>())).Returns(BuildFoos().Where(f => f.IsSane == true)).Verifiable();
  10. var viewModel = new FooListModel();
  11. viewModel.Initialize(fooController.FooRepository, new List<string>(new[] { RoleNames.Super_User }), viewModel.IsPost);
  12. var result = fooController.Index(viewModel, 1, 5, null, "", "ASC") as ViewResult;
  13. var resultModel = result.Model as FooListModel;
  14. Assert.AreEqual(1, resultModel.FooDTO.Entities.Count());

 
 

About the 14 lines of code that are really interesting:

  1. This line grabs the dummy objects from the lowermost method in the class.
  2. This line affirms that we have an unfiltered set.
  3. This line is used in the next three lines to mock the environment.
  4. Here we mock a FooController and mock its environment too by asserting that it has been accessed by way of the post verb at /foo/index
  5. This line and the one below are part of a means Byron rolled for gauging if the verb at hand is post or get.
  6. Ditto
  7. Let's fake our repository!
  8. This line does nothing ultimately, but you can see that it mocks a repository call so that it will return the same set as the first line of code.
  9. This line is used. It too mocks a repository call. If we were not mocking FindAll we would be handing in a specification. The It.IsAny thing handles the placeholder by successfully mocking anything that might actually get passed into FindAll in the FooController. The set returned is filtered down, but we control what that filtering is here, at this line of code.
  10. We have to have a model.
  11. Here we initialize the model. What is this method for? It turns out that often one will wish to do other mechanics beyond the simple model binding, hence there is a need to call a method in lieu of trying to jam a bunch of code into the getsetters for the model. viewModel.IsPost may seem a little silly here. In the controller we hand in this.IsPost which in this cause is true. We also hand in the repository and the permissions of the current user for security concerns.
  12. Let's create a view. Parameters: the model, current page in pagination, current size of pages in pagination, another model for governing searching which is not applicable here, what column we should sort on, what direction we should sort in, if the post verb is applicable.
  13. Let's get the model out of the view.
  14. Our filtered-down record set should have one item in it.

No comments:

Post a Comment