简体   繁体   English

Intellitest Pex 参数化模拟

[英]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.使用 Intellitest 和 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.当我右键单击 MethodToBeTested,然后选择运行智能测试时,预期的结果是智能测试应该实现最大代码覆盖率并使用有效的测试数据创建测试用例以覆盖 if (systemBaseObject.SystemType() == SystemConstants.SystemTypeDocument) 和 else 分支语句.

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.并使用 PexChoose 静态方法让 pex 框架探索代码以实现最大的代码覆盖率。

[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).通过上述设置,Intellitetest 可以只生成一个覆盖 if 语句的测试用例,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.可以做什么,以允许 intellitest 创建测试用例,该用例将覆盖结果值为 2 的 else 语句。

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. PexChoose 将帮助 Intellitest 探索代码,返回值将取决于原始类中 SystemConstants 的使用。 Then, create factory of ExploreMockExample using SystemBaseObject,然后,使用 SystemBaseObject 创建 ExploreMockExample 的工厂,

     [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.在实际方法 MethodToBeTested 上运行 Intellitetest,Intellitetest 将创建 2 个单元测试用例来覆盖 if else 分支语句。

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.请注意 PexAssert.AreEqual(1, i),它将覆盖 if 分支。

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.请注意 PexAssert.AreEqual(2, i),它将覆盖 else 分支。

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.使用 PexChoose.Replay.Setup() 它将返回 IPexChoiceRecorder,并且它将选择将 SystemConstants.ApplicationTypeDocument 作为参数值来覆盖 else 块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM