2017-02-23 66 views
0

我的要求是需要編寫測試用例使用的xUnit Example1視圖模型靜態方法和靜態變量。該視圖模型初始化Example2視圖模型。但Example2包含在構造函數中靜態方法和靜態方法中包含一個靜態變量。如何編寫測試用例使用包裝類或依賴注入

如果我有寫測試用例Example1,測試用例越來越失敗,同時運行所有測試案例,但同時運行選定的測試情況下獲得通過。由於在Example2內部使用了靜態方法。

我曾嘗試與改變靜態方法和變量非靜態,但其引發System.TypeInitializationException例外。

誰能解釋或提供例如此?不刪除靜態關鍵詞我怎麼能做到這一點?任何人都可以請給一個指導?

例如:

public class Example1 
{ 
    public Example1(Example2 example2) { ... } 
} 

public class Example2 
{ 
    public Example2() 
    { 
     SomeStaticMethod() //static method inside the constructor 
    } 

    static SomeStaticMethod() 
    { 
     logPath = ""; //logPath is the static variable which is declared in another static class 
    } 
} 
+0

你有什麼版本的Visual Studio?爲什麼它必須是'xunit'? – zaitsman

回答

0

如果你只是需要傳遞的Example2一個實例在構造函數中,這是相當簡單:

namespace Tests 
{ 
    using Xunit; 

    using XunitSample; 

    public class Class1 
    { 
    [Fact] 
    public void Example1_Test() 
    { 
     var ex2 = (Example2)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Example2)); 

     var target = new Example1(ex2); 

     Assert.NotNull(target); 
    } 
    } 
}