0
類
我有以下方法進行測試:鼴鼠System.Random
public float coverageJump(bool a)
{
int c = 0;
first:
c++;
Random random = new Random();
float result = random.Next(0, 100);
Console.WriteLine(result);
if(result < 50)
goto first;
if (result == 50)
goto end;
if (a)
goto end;
else
goto first;
end:
return c;
}
Pex的建議我使用Random.Next moled及其創造:
PexMethod:
[PexMethod]
public float coverageJump([PexAssumeUnderTest]ClassMethod target, bool a)
{
float result = target.coverageJump(a);
return result;
// TODO: add assertions to method ClassMethodTest.coverageJump(ClassMethod, Boolean)
}
參數化單元測試
[TestMethod]
[PexGeneratedBy(typeof(ClassMethodTest))]
[PexRaisedException(typeof(NullReferenceException))]
[HostType("Moles")]
public void coverageJumpThrowsNullReferenceException489()
{
float f;
RandomPreparation.Prepare();
ClassMethod s0 = new ClassMethod();
f = this.coverageJump(s0, false);
}
而準備的方法來嘲笑Random類:
[PexPreparationMethod(typeof(Random))]
public static void Prepare()
{
MRandom.BehaveAsCurrent();
}
我公司開發的準備方法來模擬Random類
MRandom.BehaveAsCurrent();
MRandom mr = new MRandom()
{
NextInt32Int32 = (b, c) => { return 1; },
Sample =() => { return 1; },
InternalSample =() => { return 1; }
};
MRandom.Constructor = (a) =>
{
// a.Next = (b, c) => { return 1; };
};
MRandom.Behavior = mr.InstanceBehavior;
但我得到以下NULL異常:
--- Description
failing test: NullReferenceException, Riferimento a un oggetto non impostato su un'istanza di oggetto.
float f;
RandomPreparation.Prepare();
ClassMethod s0 = new ClassMethod();
f = this.coverageJump(s0, false);
[TestMethod]
[PexGeneratedBy(typeof(ClassMethodTest))]
[PexRaisedException(typeof(NullReferenceException))]
[HostType("Moles")]
public void coverageJumpThrowsNullReferenceException387()
{
float f;
RandomPreparation.Prepare();
ClassMethod s0 = new ClassMethod();
f = this.coverageJump(s0, false);
}
異常詳細
System.NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto. at System.Int32 System.Random.InternalSample()
at System.Double System.Random.Sample()
at System.Int32 System.Random.Next(System.Int32 minValue, System.Int32 maxValue)
D:\Sviluppo\UNI\TesiTirocinio\src\TutorialsMolePex\BenchMarkTesterTool\BenchMarkTesterToolLib\ClassMethod.cs(154): at System.Single BenchMarkTesterToolLib.ClassMethod.coverageJump(System.Boolean a)
D:\Sviluppo\UNI\TesiTirocinio\src\TutorialsMolePex\BenchMarkTesterTool\BenchMarkTesterToolLib.Tests\ClassMethodTest.cs(27): at System.Single BenchMarkTesterToolLib.ClassMethodTest.coverageJump(BenchMarkTesterToolLib.ClassMethod target, System.Boolean a)
任何人都可以在這方面幫助?
真正的問題是什麼?這是作業嗎? – pickypg
嗨Pickypg,我想做一個關於Pex和痣的論文,所以我正在研究pex的行爲。問題是:「當單元測試調用Random.Next方法時,我該如何讓Rando類永遠傳遞」1「?非常感謝,最誠摯的問候。 – Boymix81
你應該看看微軟的Fakes而不是Moles。鼴鼠已被棄用,而Fakes則是其替代品(順便說一下,MS Research的同一團隊)。 http://msdn.microsoft.com/en-us/library/hh549175.aspx – Fabio