模拟Web服务并保持高代码覆盖率

时间:2014-03-06 04:05:50

标签: c# mocking

目前我的界面看起来像

interface IMyInterface
{
    public int GetValue(string name);

}

我嘲笑如下:

class MyMockValid : IMyInterface
{
    public int GetValue(string name)
    {
        return 100;
    }

}

public testValidValue
{
    IMyInterface interface = new MyMockValid();
    Assert.IsEqual(100, interface.GetValue("Micheal");
}

现在问题是当我尝试编写实际来自Web服务的代码时,我无法将代码覆盖率设置为100%,因为代码永远不会运行。如:

class MyClass : IMyInterface
{
    public int GetValue(string name)
    {
        //Do HTTPREQ using Name
        // Return the value from webservice
        return ValueFromService;
    }
}

class MyLibrary
{
    IMyInterface _interface;
    MyLibrary() { _interface = new MyClass(); }

    MyLibrary(IMyInterface Mockinterface)
    {
        _interface = Mockinterface;
    }

    public GetVal(string name)
    {
        return _interface.GetValue(name);
    }
}

当服务中的GetValue由于模拟生效而从未运行时,如何保持良好的代码覆盖率?

0 个答案:

没有答案
相关问题