2012-10-10 24 views
2

我正在寫一些N-Unit測試,而且我有點困難。我試圖在我的代碼中將Test連接到TestCaseSource,但它似乎沒有正確構建對象。N-Unit中的TestCaseSource

這裏是我的測試方法:

[Test, TestCaseSource(typeof(TestCaseStuff), "GetTestCases", Category = "Release")] 
    public void WithdrawMoney(TestCaseStuff tc) 
    { 
     double newBalance = AccountBalance - tc.amt; 
     if (newBalance < 0) 
     { 
      Assert.Fail(String.Format("You can't withdraw {0}! You've maxed" + 
       "out your bank account.", tc.amt.ToString("C2"))); 
     } 

     AccountBalance = newBalance; 
     Assert.Pass(String.Format("Your new balance is {0}.", 
     (AccountBalance).ToString("C2"))); 
    } 

而且我TestCaseSource

public class TestCaseStuff 
{ 
    public double amt { get; set; } 

    public TestCaseStuff() 
    { 
     Random rnd = new Random(); 
     this.amt = rnd.Next(0, 20); 
    } 

    [Category("Release")] 
    public IEnumerable<TestCaseData> GetTestCases() 
    { 
     for (int i = 0; i < 500; i++) 
     { 
      yield return new TestCaseData(this); 
     } 
    } 
} 

的主要是作爲概念驗證,這樣,當它的時間來寫複雜的實際測試對象,我會知道我可以只寫一個上面的循環,並將隨機值放入我的對象中。但是,返回到我的測試方法的TestCaseStuff的每個實例都是相同的。

更新:

下面的答案是正確的。當我將它傳遞給N-Units TestCaseData對象時,我認爲(錯誤地)它只是簡單地按值傳遞該實例。顯然,它是通過引用完成的,這就是爲什麼值總是相同的原因。

最重要的是,我錯誤地使用了Random類。這不是我經常處理的事情,但我沒有正確地閱讀它。正如下面的鏈接中所解釋的,當使用Random以及它的默認構造函數時,種子值是從系統時鐘派生的。因此,如果您快速連續實例化幾個Random對象,它們將共享相同的默認種子值並生成相同的值。

因此,作爲這些發展的結果,我的代碼,原先爲:

public class TestCaseStuff 
{ 
    public double amt { get; set; } 
    private static readonly Random rnd = new Random(); 

    public TestCaseStuff() 
    { 
     this.amt = rnd.Next(0, 20); 
    } 

    [Category("Release")] 
    public IEnumerable<TestCaseData> GetTestCases() 
    { 
     for (int i = 0; i < 500; i++) 
     { 
      yield return new TestCaseData(new TestCaseStuff()); 
     } 
    } 

} 

回答

3

上面的代碼將只實例化的TestCaseSource一個拷貝,並保持餵養相同的值。

如果意圖是要在每個循環中生成一個隨機數,我認爲您應該創建一個對象的靜態實例,並在每次創建新的TestCaseStuff對象時從其生成一個數字。

我會改寫TestCaseStuff這樣的:

public class TestCaseStuff 
{ 
    // Static instance of a random number generator. 
    private static readonly Random RNG = new Random(); 

    public double amt { get; set; } 

    public TestCaseStuff() 
    { 
     this.amt = RNG.Next(0, 20); 
    } 

    [Category("Release")] 
    public IEnumerable<TestCaseData> GetTestCases() 
    { 
     for (int i = 0; i < 500; i++) 
     { 
      // NOTE: You need to create a new instance here. 
      yield return new TestCaseData(new TestCaseStuff()); 
     } 
    } 
} 
+0

是的,那就是我最終做的。我認爲TestCaseData在我傳入'this'時會有它自己的TestCaseStuff副本,但我顯然錯了。 – elucid8

相關問題