代码覆盖率失败使用简单的类构造函数进行测试

时间:2014-01-31 15:33:47

标签: unit-testing c#-4.0 code-coverage nmock

我有一个班级:

 public class SourceServerProvider : ISourceServerProvider
    {
        private readonly ISourceServer _sourceServer;
        public SourceServerProvider()
            :this(new SourceServer())
        { }

        public SourceServerProvider(ISourceServer sourceServer)
        {
            _sourceServer = sourceServer;
        }
     }

MS代码覆盖测试对此块的投诉:

public SourceServerProvider()
            :this(new SourceServer())
        { }

我不知道如何为上面的块编写单元测试。请指教..

1 个答案:

答案 0 :(得分:0)

我刚用以下代码测试了它:

public class SourceServerProvider : ISourceServerProvider
{
private readonly ISourceServer _sourceServer;
public SourceServerProvider()
    : this(new SourceServer())
{ }

public SourceServerProvider(ISourceServer sourceServer)
{
    _sourceServer = sourceServer;
}
}

public interface ISourceServer
{
}

public class SourceServer : ISourceServer
{
}

public interface ISourceServerProvider
{
}

并写了这个测试

public class Class1
{
    [Test]
    public void test()
    {
        var a = new SourceServerProvider();
    }
}

代码覆盖率表示完全覆盖: here

所以请添加您获得的结果或创建调用默认ctor的简单测试