4

I'm using the Analyse Code Coverage tool in Visual Studio 2012. The report appears to be very pedantic about what is covered and I have no idea what else could be done to provide more coverage.

The code I am testing is this:

public class Factory<T> : IFactory<T> where T : new()
{
    public T Create()
    {
        return new T();  // This line has only partial coverage.
    }
}

The unit tests:

using System;
using Xunit;
public class Factory_Tests
{
    [Fact]
    public void Constructor_Works()
    {
        var target = new Factory<Exception>();
    }

    [Fact]
    public void Create_ReturnsNewValue()
    {
        var target = new Factory<Exception>();
        var actual = target.Create();
        Assert.NotNull(actual);
    }
}

The report claims that the line commented above has only partial coverage. What could I have possibly failed to test on this line?

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
  • 2
    I would guess that due to it being generic, you haven't tested it with all the type of types that are allowed--i.e. you used a reference type, perhaps it wants you to use a value type as well. – Matt Smith Mar 19 '14 at 02:12
  • 1
    I don't know C#, but isn't it pretty useless to simple check whether a method does not return null? – Niklas B. Mar 19 '14 at 02:17
  • 1
    @MattSmith, yup, you've got it. Thanks! – Hand-E-Food Mar 19 '14 at 02:21
  • @NiklasB., not if the method guarantees it returns a value and returning null would cause a NullReferenceException elsewhere. Yes, this is a very mundane test for a very mundane piece of code. It's a personal exercise in not being test-lazy. – Hand-E-Food Mar 19 '14 at 02:21
  • @Hand-E-Food Okay, but won't you test the *functionality* of the created object somewhere? That would certainly catch the NPE as well :) Anyway, it's not important for the question – Niklas B. Mar 19 '14 at 02:24

1 Answers1

2

Since it is a generic method that can accept both a reference type and a value type, it wants you test it with both.

Matt Smith
  • 17,026
  • 7
  • 53
  • 103