locked
Pex report PexAssumeFailedException in factory method RRS feed

  • Question

  • Hey, every one! 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.
    I just want to test a abstract class method FontData.sGetTextLine(), the code is shown below:
    namespace Root.Reports {
      internal abstract class FontData {
        /// <summary>Gets a text line for the specified width.</summary>
        /// <param name="sText">Text</param>
        /// <param name="rWidthMax">Width</param>
        /// <param name="iStart">Start position in sText</param>
        /// <param name="textSplitMode">Text split mode</param>
        /// <returns>Line of text</returns>
        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);
         
          // ...
         }
      }
    }
    

    Then Pex hlep me to generate the parameterized Unit tests method FontDataTest.sGetTextLine(), the code is shown below:
    namespace Root.Reports
    {
        /// <summary>This class contains parameterized unit tests for FontData</summary>
        [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)
            }
        }
    }
    
     After I run the Pex Exploration, Pex show a message "Guess how to create Root.Reports.Type1FontData". Then I Click the "Edit Factory" and then create a class Type1FontDataFactory, the code is
    namespace Root.Reports
    {
        /// <summary>This class contains parameterized unit tests for FontData</summary>
        [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)
            }
        }
    }
    

     then I run the Pex Exploration again, and Pex tell me "could not create an instance of Root.Reports.Type1FontData", and the details message Pex reported is:
     
     
    My question is how to hanle this problem, please help me! 
     
    Thank you all !
     
     Additionally, the calss Type1FontData is a concrete class based on abstract FontData class, 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)
        {
           // .....
        }
      }
    }
    
     
     
     
    Sunday, December 18, 2011 5:00 AM

All replies

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

         I'm sorry for missing the details message Pex reported, here is it;

     ----------------------------------------------------------------------------------------------------------------------------------

    [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
    

     

    Additionally, the dynamic code coverage summay show that the user code or test only cover the following code in Type1FontDataFactory.Create()

     ---------------------------------------------------------------------------------------------------------------------------------

     

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

     


    • Edited by willcn Sunday, December 18, 2011 5:13 AM
    Sunday, December 18, 2011 5:12 AM