大家!我是單元測試的初學者。我現在正在使用visual studio 2010和Pex測試一個項目,而且我遇到了以下問題。Pex的報告PexAssumeFailedException在工廠方法
當我運行Pex的探索,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:FontDataTest.sGetTextLine(FontData,字符串,雙,的Int32 &,TextSplitMode)在段索引0的需求類型Type1Fo的對象ntData(某個對象#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. calss'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&, 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的探索後,PEX顯示一條消息,「猜猜如何創建Root.Reports.Type1FontData」。然後我點擊「編輯工廠」和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的探索,然後我得到了PexAssumeFailedException「不能創建一個Root.Reports.Type1FontData的實例「。
6所示的用戶代碼或測試Type1FontDataFactory.Create()的動態代碼覆蓋summay只涵蓋以下代碼:
Type1FontData type1FontData
= new Type1FontData(fontDef_fontDef, _sFontName_s, fontStyle_i);
我的問題是如何漢勒這個問題,請幫我!
謝謝大家!