2017-10-18 95 views
0

我試圖使用ValueSourceAttribute進行測試。在ValueSourceAttribute上指定的sourceName必須引用非空靜態字段,屬性或方法

下面是一個例子

[Test] 
     public async Task TestDocumentsDifferentFormats(
      [ValueSource(nameof(Formats))] string format, 
      [ValueSource(nameof(Documents))] IDocument document) 
     { 

有趣的是,Formats名單(第一個參數)的作品完美,但它解決不了第二個參數,即使以同樣的方式定義。

這是我如何定義文檔靜態列表

public class DocumentFactory 
    { 
     public static readonly List<IDocument> Documents= 
      new List<IDocument> 
      { 
       // Init documents 
      }; 
    } 

但是當我嘗試運行我的測試中,它拋出一個錯誤。

The sourceName specified on a ValueSourceAttribute must refer to a non null static field, property or method. 

什麼會導致此問題?我會很感激任何幫助。

+0

您應該提到只有在另一個類中聲明'Documents'屬性時纔會出現此問題。 – Fabio

+0

@Fabio是的,它是在另一個類中聲明的,是否有可能解決這個問題? – bxfvgekd

回答

1

如果值是在另一個類中定義的,你應該提供它作爲參數太鍵入屬性

[Test] 
public void TestOne(
    [ValueSource(nameof(Formats))] string format, 
    [ValueSource(typeof(DocumentFactory), nameof(DocumentFactory.Documents))] IDocument document) 
{ 
     document.Should().NotBeNull(); 
} 

沒有提供一個類型,NUnit的將使用當前類的默認類型的類型,這就是爲什麼Formats作品。

+0

工程就像一個魅力。謝謝你指出這個錯誤。 – bxfvgekd

相關問題