2012-09-15 24 views
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) 

任何人都可以在這方面幫助?

+0

真正的問題是什麼?這是作業嗎? – pickypg

+0

嗨Pickypg,我想做一個關於Pex和痣的論文,所以我正在研究pex的行爲。問題是:「當單元測試調用Random.Next方法時,我該如何讓Rando類永遠傳遞」1「?非常感謝,最誠摯的問候。 – Boymix81

+0

你應該看看微軟的Fakes而不是Moles。鼴鼠已被棄用,而Fakes則是其替代品(順便說一下,MS Research的同一團隊)。 http://msdn.microsoft.com/en-us/library/hh549175.aspx – Fabio

回答

0

我覺得這是解決我的問題在準備方法:)

準備(

[PexPreparationMethod(typeof(Random))] 
    public static void Prepare() 
    { 
     MRandom.Behavior = MoleBehaviors.Fallthrough; 
     MRandom.AllInstances.NextInt32Int32 = (r, a, b) => { return 50; }; 
    } 

我想使用另一種實現

 var fake = new SRandom { CallBase = true }; 
     var mole = new MRandom(fake) 
     { 
      NextInt32Int32 = (a, b) => { return 1; } 
     }; 
     MRandom.BehaveAsCurrent(); 

,但不起作用。有一段時間,我認爲我不能嘲笑隨機的方法下一步,因爲在鼴鼠手動參考報告: ..痣框架來創建鼴鼠類型,利用運行時儀器。用靜態方法,密封類型,或非虛方法的API打交道時,他們是有用的..

和隨機的方法,接下來是:

 public virtual int Next(
      int minValue, 
      int maxValue 
     ) 

可能有人給我一個解釋,因爲我的「準備」方法工作?

非常感謝,最好的問候。