Saturday, November 12, 2011

I'm learning to use It.Is within MOQ mocking in lieu of the restrictive straightjacket of It.IsAny

See the lines in white below. They matter the most.

 
 

[TestMethod]

public void MayProduceListOfAdminAreaRolesForSampleTypes()

{

   
//arrange

   SampleType cpu = new SampleType{ Name = "CPU", Id =

         new Guid("afd22e87-1ce0-4b5c-b0fa-9e4000a8b28b") };

   SampleType gpu = new SampleType{ Name = "GPU", Id =

         new Guid("edd8a447-43a3-418b-9cd2-9e4000a8b290") };

   SampleType chipset = new SampleType{ Name = "Chipset", Id =

         new Guid("fa4c3b36-675f-4df3-8165-9e4000a8b28c") };

   var person = TestObjects.BuildPerson("Test", "Person");

   person.Profile = TestObjects.BuildProfile("username");

   var profileId = Guid.NewGuid();

   person.Profile.Id = profileId;

   IList<string> roles = new List<string> { "PROGRAM_CREATE",

         "SILICON.CPU_ADMIN", "SILICON.CHIPSET_ADMIN" };

   var authorizationService = new Mock<IAuthorizationService>();

   authorizationService.Setup(repo =>

         repo.DoesMatchRoleForSampleTypes(It.IsAny<List<string>>(),

         It.Is<SampleType>(st => st == cpu), It.IsAny<string>())).Returns(true);

   authorizationService.Setup(repo =>

         repo.DoesMatchRoleForSampleTypes(It.IsAny<List<string>>(),

         It.Is<SampleType>(st => st == gpu), It.IsAny<string>())).Returns(false);

   authorizationService.Setup(repo =>

         repo.DoesMatchRoleForSampleTypes(It.IsAny<List<string>>(),

         It.Is<SampleType>(st => st == chipset), It.IsAny<string>())).Returns(true);


   _accessMapBuilder.AuthorizationService = authorizationService.Object;

   var stRepository = new Mock<ISampleTypeRepository>();

   stRepository.Setup(repo => repo.GetAllActiveSiliconSampleTypes()).Returns(new

         List<SampleType>(){cpu,gpu,chipset}.AsQueryable);

   _accessMapBuilder.SampleTypeRepository = stRepository.Object;

   

   
//act

   IList<Guid> results =

         _accessMapBuilder.BuildListOfSampleTypesForWhichUserIsAdmin(roles);

   

   
//assert

   Assert.AreEqual(results.Count,2);

}

No comments:

Post a Comment