2015-05-13 63 views
1

我有以下抽象類,我想編寫一個單元測試。我是微軟假冒新手,到目前爲止我只用它來測試公共課程。如何使用Microsft Fakes框架單元測試抽象類

public abstract class ProvideBase 
{ 
    private string tag = string.Empty; 

    public string Tag 
    { 
     get { return tag; }   
     set { tag = value; } 
    } 

} 

public static String GetMyConfig(string sectionName) 
{ 
    MyConfiguration config = MyConfiguration.GetConfig(sectionName); 
    return config.GetMyConfig(config.DefaultConfig); 
} 

我寫一個單元測試,我GetMyConfig()方法。但是我的測試覆蓋率不是100%,因爲我沒有使用Tag屬性。有沒有一種方法可以測試它?

Pex做了一些嘲笑來測試這樣的事情。如何使用Microsoft Fakes模擬/測試Tag屬性?

+2

100%的測試覆蓋率並不那麼重要。你不需要測試每個簡單的屬性返回你剛剛設置的值。 – Blorgbeard

回答

2

我真的不知道爲什麼要使用正版正貨承諾這一點。從中派生出一個班級,可以輕鬆測試:

class TestableProvideBase : ProvideBase{} 

[TestMethod] 
public void TestTagProperty() { 
    var sut = new TestableProvideBase(); 

    Assert.AreEqual(String.Empty, sut.Tag); 

    sut.Tag = "someValue"; 

    Assert.AreEqual("someValue", sut.Tag); 
} 
0

嘲笑父類的屬性,我使用下面的語法

bool tagPropertyGot = false; 
ShimProvideBase.AllInstances.TagGet = (i) => { 
    if (i.Equals(myTestingTargetInstance)) 
    { 
     tagPropertyGot = true; 
     return "otherTagValue"; 
    } 
    return "tagvalue"; 
};