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:
- IEnumerable<Foo> foos = BuildFoos();
- Assert.AreEqual(2, foos.Count());
- var url = "/foo/index";
- var fooController = TestUtils.BuildTestController<FooController>("POST", url);
- fooController.Session["PrevUrl"] = url;
- fooController.Session["CurUrl"] = url;
- fooController.FooRepository = ServiceLocator.Get<IFooRepository>(AppCtxIds.FOO_REPOSITORY);
- registry.FooRepositoryMock.Setup(repo => repo.GetAll()).Returns(BuildFoos()).Verifiable();
- registry.FooRepositoryMock.Setup(repo => repo.FindAll(It.IsAny<Specification<Foo>>())).Returns(BuildFoos().Where(f => f.IsSane == true)).Verifiable();
- var viewModel = new FooListModel();
- viewModel.Initialize(fooController.FooRepository, new List<string>(new[] { RoleNames.Super_User }), viewModel.IsPost);
- var result = fooController.Index(viewModel, 1, 5, null, "", "ASC") as ViewResult;
- var resultModel = result.Model as FooListModel;
- Assert.AreEqual(1, resultModel.FooDTO.Entities.Count());
About the 14 lines of code that are really interesting:
- This line grabs the dummy objects from the lowermost method in the class.
- This line affirms that we have an unfiltered set.
- This line is used in the next three lines to mock the environment.
- 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
- This line and the one below are part of a means Byron rolled for gauging if the verb at hand is post or get.
- Ditto
- Let's fake our repository!
- 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.
- 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.
- We have to have a model.
- 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.
- 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.
- Let's get the model out of the view.
- Our filtered-down record set should have one item in it.
No comments:
Post a Comment