cec*_*lip 2 unit-testing microsoft-fakes
我有一个我需要模拟的接口,它具有部分看起来像这样的索引器属性.
public interface MyInterface{
string this[string name] {get;set;};
string this[int index] {get;set;};
}
Run Code Online (Sandbox Code Playgroud)
我想模拟界面,以便上面的名称和索引的某些值返回我提供的值.如何使用Microsoft Fakes Framework实现这一目标?
小智 5
您可以简单地利用Microsoft Fakes在测试中存根此功能.右键单击目标程序集(包含接口定义的项目),然后在测试项目的引用中选择add Fakes assembly.
生成的伪组件将是"TargetAssembly.Fakes".在该程序集中,您将拥有一个带有方法的"StubMyInterface"类型,"ItemGetInt32","ItemGetString","ItemSetInt32String","ItemSetStringString",这是3个get/set方法的存根实现.
您可以在测试中使用它们,如下所示.
[TestMethod]
public void MyInterfaceTest()
{
var stub = new StubMyInterface()
{
ItemGetInt32 = (x) => { return "teststring"; }
};
MyInterface SUT = stub;
var result = SUT[47];
Assert.AreEqual("teststring", result);
}
Run Code Online (Sandbox Code Playgroud)