Pex 在工厂方法中报告 PexAssumeFailedException

发布于 2024-12-21 14:10:11 字数 4572 浏览 1 评论 0原文

每个人!我是单元测试的初学者。我现在正在使用 Visual Studio 2010 和 Pex 测试一个项目,遇到以下问题。

当我运行 Pex Exploration 时,Pex 告诉我“无法创建 Root.Reports.Type1FontData 的实例”,Pex 报告的详细信息是:


[TestMethod]
[PexGeneratedBy(typeof(FontDataTest))]
[Ignore]
[PexDescription("the test state was: assumption violation")]
public void sGetTextLineThrowsPexAssumeFailedException500()
{
    string s;
    int i = 0;
    s =
    this.sGetTextLine((FontData)null, (string)null, 0, ref i, TextSplitMode.Line);
}

--- 异常详细信息

Microsoft.Pex.Framework .Exceptions.PexAssumeFailedException:段索引 0 处需要 FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode) Type1FontData 类型的对象(某个对象#0); ExSig#1



1 扩展序列。我只是想测试一个抽象类的方法'FontData.sGetTextLine()',代码如下:

namespace Root.Reports {
    internal abstract class FontData {
         internal String sGetTextLine(String sText, Double rWidthMax, ref Int32 iStart, TextSplitMode textSplitMode) {
             if (iStart > sText.Length) {
                 throw new ReportException("start position out of range");
             }
             if (iStart == sText.Length) {
                 iStart++;
                 return "";
             }
             Int32 iStartCopy = iStart;

             StringBuilder sb = new StringBuilder(120);

             // ...
        }
    }

}

2.类'Type1FontData'是一个基于抽象类'FontData'的具体类,Type1FontData的代码为:

namespace Root.Reports {
   internal class Type1FontData : FontData {
      internal Type1FontData(FontDef fontDef, String _sFontName, FontStyle fontStyle)
      : base(fontDef, fontStyle, FontData.Encoding.Cp1252)
   {
      // .....
   }
 }

}

3.然后Pex帮我生成一个PUT类方法'FontDataTest.sGetTextLine()':

namespace Root.Reports {
  [PexClass(typeof(FontData))]
  [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
  [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
  [TestClass]
  public partial class FontDataTest
  {
    /// <summary>Test stub for sGetTextLine(String, Double, Int32&amp;, TextSplitMode)</summary>
    [PexMethod(TestEmissionFilter= PexTestEmissionFilter.All)]
    internal string sGetTextLine(
        [PexAssumeNotNull]FontData target,
        string sText,
        double rWidthMax,
        ref int iStart,
        TextSplitMode textSplitMode
    )
    {
        string result = target.sGetTextLine(sText, rWidthMax, ref iStart, textSplitMode);
        return result;
        // TODO: add assertions to method FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode)
    }
   }
 }

}

4。运行 Pex Exploration 后,Pex 显示一条消息“猜测如何创建 Root.Reports.Type1FontData”。然后我点击“Edit Factory”,Pex创建一个类Type1FontDataFactory,代码为

namespace Root.Reports
{
public static partial class Type1FontDataFactory
{
    [PexFactoryMethod(typeof(RT), "Root.Reports.Type1FontData")]
    public static Type1FontData Create(
        FontDef fontDef_fontDef,
        string _sFontName_s,
        FontStyle fontStyle_i,
        FontDef fontDef_fontDef1,
        object oFontDataX_o,
        BitArray bitArray_UsedChar_bitArray
    )
    {
        Type1FontData type1FontData
           = new Type1FontData(fontDef_fontDef, _sFontName_s, fontStyle_i);
        type1FontData.fontDef = fontDef_fontDef1;
        type1FontData.oFontDataX = oFontDataX_o;
        type1FontData.bitArray_UsedChar = bitArray_UsedChar_bitArray;
        return type1FontData;

        // TODO: Edit factory method of Type1FontData
        // This method should be able to configure the object in all possible ways.
        // Add as many parameters as needed,
        // and assign their values to each field by using the API.
    }
}

}

5。当我再次运行 Pex Exploration 时,出现 PexAssumeFailedException“无法创建 Root.Reports.Type1FontData 的实例”。

6。 Type1FontDataFactory.Create() 的动态代码覆盖率摘要显示的用户代码或测试仅覆盖以下代码:

Type1FontData type1FontData
           = new Type1FontData(fontDef_fontDef, _sFontName_s, fontStyle_i);

我的问题是如何处理这个问题,请帮助我!
谢谢大家!

everyone! I am a beginner in unit testing. I'm now testing a project with visual studio 2010 and Pex, and I encountered the following problem.

When I run the Pex Exploration, and Pex tell me "could not create an instance of Root.Reports.Type1FontData", and the details message Pex reported is:


[TestMethod]
[PexGeneratedBy(typeof(FontDataTest))]
[Ignore]
[PexDescription("the test state was: assumption violation")]
public void sGetTextLineThrowsPexAssumeFailedException500()
{
    string s;
    int i = 0;
    s =
    this.sGetTextLine((FontData)null, (string)null, 0, ref i, TextSplitMode.Line);
}

--- Exception details

Microsoft.Pex.Framework.Exceptions.PexAssumeFailedException: FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode) at segment index 0 needs object of type Type1FontData (some object#0); extended sequence by ExSig#1



1. I just want to test a abstract class's method 'FontData.sGetTextLine()', the code is shown below:

namespace Root.Reports {
    internal abstract class FontData {
         internal String sGetTextLine(String sText, Double rWidthMax, ref Int32 iStart, TextSplitMode textSplitMode) {
             if (iStart > sText.Length) {
                 throw new ReportException("start position out of range");
             }
             if (iStart == sText.Length) {
                 iStart++;
                 return "";
             }
             Int32 iStartCopy = iStart;

             StringBuilder sb = new StringBuilder(120);

             // ...
        }
    }

}

2. The calss 'Type1FontData' is a concrete class based on abstract class 'FontData', and the code of Type1FontData is:

namespace Root.Reports {
   internal class Type1FontData : FontData {
      internal Type1FontData(FontDef fontDef, String _sFontName, FontStyle fontStyle)
      : base(fontDef, fontStyle, FontData.Encoding.Cp1252)
   {
      // .....
   }
 }

}

3. Then Pex help me to generate a PUT class method 'FontDataTest.sGetTextLine() ':

namespace Root.Reports {
  [PexClass(typeof(FontData))]
  [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
  [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
  [TestClass]
  public partial class FontDataTest
  {
    /// <summary>Test stub for sGetTextLine(String, Double, Int32&, TextSplitMode)</summary>
    [PexMethod(TestEmissionFilter= PexTestEmissionFilter.All)]
    internal string sGetTextLine(
        [PexAssumeNotNull]FontData target,
        string sText,
        double rWidthMax,
        ref int iStart,
        TextSplitMode textSplitMode
    )
    {
        string result = target.sGetTextLine(sText, rWidthMax, ref iStart, textSplitMode);
        return result;
        // TODO: add assertions to method FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode)
    }
   }
 }

}

4. After I run the Pex Exploration, Pex show a message "Guess how to create Root.Reports.Type1FontData". Then I Click the "Edit Factory" and Pex create a class Type1FontDataFactory, the code is

namespace Root.Reports
{
public static partial class Type1FontDataFactory
{
    [PexFactoryMethod(typeof(RT), "Root.Reports.Type1FontData")]
    public static Type1FontData Create(
        FontDef fontDef_fontDef,
        string _sFontName_s,
        FontStyle fontStyle_i,
        FontDef fontDef_fontDef1,
        object oFontDataX_o,
        BitArray bitArray_UsedChar_bitArray
    )
    {
        Type1FontData type1FontData
           = new Type1FontData(fontDef_fontDef, _sFontName_s, fontStyle_i);
        type1FontData.fontDef = fontDef_fontDef1;
        type1FontData.oFontDataX = oFontDataX_o;
        type1FontData.bitArray_UsedChar = bitArray_UsedChar_bitArray;
        return type1FontData;

        // TODO: Edit factory method of Type1FontData
        // This method should be able to configure the object in all possible ways.
        // Add as many parameters as needed,
        // and assign their values to each field by using the API.
    }
}

}

5. When I run the Pex Exploration again, then I got the PexAssumeFailedException "could not create an instance of Root.Reports.Type1FontData".

6. the dynamic code coverage summay of Type1FontDataFactory.Create() shown the user code or test only covered the following code:

Type1FontData type1FontData
           = new Type1FontData(fontDef_fontDef, _sFontName_s, fontStyle_i);

My question is how to hanle this problem, please help me!
Thank you all !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知足的幸福 2024-12-28 14:10:11

PexFactory 方法用于创建复杂类型的实例。在您的情况下 FontData 是抽象类。您也不能创建抽象类的实例。
在方法调用之前使用以下属性。

[PexUseType(typeof(Type1FontData ))]
[PexMethod(TestEmissionFilter= PexTestEmissionFilter.All)]
     internal string sGetTextLine(
         [PexAssumeNotNull]FontData target,
         string sText,
         double rWidthMax,
         ref int iStart,
         TextSplitMode textSplitMode 
     )
     {
        string result = target.sGetTextLine(sText, rWidthMax, ref iStart, textSplitMode);
        return result;
        // TODO: add assertions to method 
        FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode)
    }    
    }
}

PexFactory method is use to create instance of complex type.In your case FontData is abstract class.You can nor create instance of abstract class.
Use below attribute before method call.

[PexUseType(typeof(Type1FontData ))]
[PexMethod(TestEmissionFilter= PexTestEmissionFilter.All)]
     internal string sGetTextLine(
         [PexAssumeNotNull]FontData target,
         string sText,
         double rWidthMax,
         ref int iStart,
         TextSplitMode textSplitMode 
     )
     {
        string result = target.sGetTextLine(sText, rWidthMax, ref iStart, textSplitMode);
        return result;
        // TODO: add assertions to method 
        FontDataTest.sGetTextLine(FontData, String, Double, Int32&, TextSplitMode)
    }    
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文