[英]How to unit test an abstract class using Microsft Fakes framework
I have the following abstract class for which I want to write a unit test. 我有以下要为其编写单元测试的抽象类。 I am new to Microsoft Fakes and so far I have only used it for testing public classes. 我是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);
}
I wrote a unit test for my GetMyConfig()
method. 我为GetMyConfig()
方法编写了一个单元测试。 My test coverage is not 100% however, since I have not used the Tag
property. 我的测试覆盖率不是100%,因为我没有使用Tag
属性。 Is there a way I can test it to? 有办法测试吗?
Pex does some kind of mocking to test such things. Pex进行某种模拟来测试这些东西。 How do I mock/test the Tag
property using Microsoft Fakes? 如何使用Microsoft Fakes模拟/测试Tag
属性?
I'm not really sure why you want to use Fakes for this. 我不太确定为什么要为此使用Fakes。 Deriving a class from it makes it easy enough to test: 从中派生一个类使测试变得很容易:
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);
}
to mock a property of the parent class I am using the following syntax 模仿父类的属性,我正在使用以下语法
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.