[英]How to unit test an abstract class using Microsft Fakes framework
我有以下要為其編寫單元測試的抽象類。 我是Microsoft 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
屬性?
我不太確定為什么要為此使用Fakes。 從中派生一個類使測試變得很容易:
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);
}
模仿父類的屬性,我正在使用以下語法
bool tagPropertyGot = false;
ShimProvideBase.AllInstances.TagGet = (i) => {
if (i.Equals(myTestingTargetInstance))
{
tagPropertyGot = true;
return "otherTagValue";
}
return "tagvalue";
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.