Tuesday, October 11, 2011

another mocking example

[TestClass]

public class MyTest

{

   private FooBuilder fooBuilder;

   

   [TestInitialize]

   public void Setup()

   {

      SessionObjectCache.Remove(CachedEntries.USER_CONTEXT);

      var registry = new MocksRegistry();

      registry.BarRepositoryMock.Setup(repo =>

            repo.GetAll()).Returns(new List<Bar>().AsQueryable());

      registry.BazServiceMock.Setup(repo =>

            repo.DoesMatchAnyBazForBars(It.IsAny<List<Bar>>()));

      fooBuilder = new FooBuilder

      {

         BarRepository = registry.BarRepositoryMock.Object,

         BazService = registry.BazServiceMock.Object

      };

      var testServices = new Dictionary<string, object>();

      testServices[AppCtxIds.FOO_BUILDER] = fooBuilder;

      ServiceLocator.LookupSource = () => testServices;

      }

   

   [TestMethod]

   public void LetsTest()

   {

      var qux = BuildQux("Tom");

      var userContext = SessionObjectCache.LazyGet<UserContext>

            (CachedEntries.USER_CONTEXT,

            new Func<UserContext>(() =>

            {

               return new UserContext { Qux = qux };

            }));

      Assert.IsTrue(SessionObjectCache.Exists(CachedEntries.USER_CONTEXT));

      Assert.AreEqual("Tom", SessionObjectCache.Get<UserContext>

            (CachedEntries.USER_CONTEXT).Qux.Name);

   }

   

   private Qux BuildQux(string whatever)

   {

      return fooBuilder.Build(whatever);

   }

   

   private class TestController : Controller

   {

   

   }

}

No comments:

Post a Comment