Search code examples
pexintellitest

Intellitest Pex Parameterize Mock


public enum SystemConstants
 {
     SystemTypeDocument,
     ApplicationTypeDocument
 }

public interface ISystemBaseObject
 {
     SystemConstants SystemType();
 }

 public class ExploreMockExample
 {
     ISystemBaseObject systemBaseObject;
     public ExploreMockExample(ISystemBaseObject systemObject)
     {
         systemBaseObject = systemObject;
     }
     public int MethodToBeTested()
     {
         if (systemBaseObject.SystemType() == SystemConstants.SystemTypeDocument)
         {
             return 1;
         }
         else
         {
             return 2;
         }
     }
 }

Using intellitest along with NUnit3.

When I right click MethodToBeTested, and then select run intellitest, expected outcome is Intellitest test should achieve maximum code coverage and create test case with valid test data to cover both if (systemBaseObject.SystemType() == SystemConstants.SystemTypeDocument) and else branch statement.

Some blogs suggested to create factory for class and create mock object of interface. And use PexChoose static method to allow pex framework to explore code to achieve maximum code coverage.

[PexFactoryMethod(typeof(ExploreMockExample))]
     public static ExploreMockExample CreateMock()
     {
         var mockComosBaseObject = new Mock<ISystemBaseObject>();
         mockComosBaseObject.Setup(c =>c.SystemType()).
             Returns(PexChoose.EnumValue<SystemConstants>(nameof(SystemConstants)));
         return new ExploreMockExample(mockComosBaseObject.Object);            
     }

With above setup, Intellitest could above to generate only one test case which is covering if statement, if (systemBaseObject.SystemType() == SystemConstants.SystemTypeDocument).

What can be done, to allow intellitest to create test case which will cover else statement having result value as 2.


Solution

  • Create mock implementation for your interface. Like mentioned below,

         public class SystemBaseObject : ISystemBaseObject
         {
             public SystemConstants SystemType()
             {
                 return PexChoose.EnumValue<SystemConstants>("SystemConstants");
             }
         }
    

    PexChoose will help intellitest to explore code, return value will depend upon uses of SystemConstants in original class. Then, create factory of ExploreMockExample using SystemBaseObject,

         [PexFactoryMethod(typeof(ExploreMockExample))]
         public static ExploreMockExample Create()
         {
             return new ExploreMockExample(new SystemBaseObject());            
         }
    

    Run Intellitest on actual method MethodToBeTested, Intellitest will create 2 unit test case to cover both if else branch statement.

    First test case,

         [PexGeneratedBy(typeof(ExploreMockExampleTest))]
         public void MethodToBeTested806()
         {
             ExploreMockExample exploreMockExample;
             int i;
             exploreMockExample = ExploreMockExampleFactory.Create();
             i = this.MethodToBeTested(exploreMockExample);
             PexAssert.AreEqual<int>(1, i);
             PexAssert.IsNotNull((object)exploreMockExample);
         }
    

    Kindly observe PexAssert.AreEqual(1, i), which will cover if branch.

    Second test case,

         [PexGeneratedBy(typeof(ExploreMockExampleTest))]
         public void MethodToBeTested792()
         {
             ExploreMockExample exploreMockExample;
             int i;
             exploreMockExample = ExploreMockExampleFactory.Create();
             IPexChoiceRecorder choices = PexChoose.Replay.Setup();
             choices.NextSegment(1).DefaultSession
                 .At(0, "SystemConstants", (object)(SystemConstants.ApplicationTypeDocument));
             i = this.MethodToBeTested(exploreMockExample);
             PexAssert.AreEqual<int>(2, i);
             PexAssert.IsNotNull((object)exploreMockExample);
         }
    

    Kindly observe PexAssert.AreEqual(2, i), which will cover else branch.

    Using PexChoose.Replay.Setup() it will return IPexChoiceRecorder, and it will make choice about to have SystemConstants.ApplicationTypeDocument as argument value to cover else block.