テストする次の方法があります。
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 とその create を使用することを提案しています:
Pex メソッド :
[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 Exceptionが発生します。
--- 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)
誰でもこれを手伝ってもらえますか?