Which block is not covered?

Visual Studio 2013 shows my code coverage for this (simplified for this example) object as a missing block:

code coverage

As far as I know, it if

should have exactly two states. Come through and fail. And debugging my tests shows that each of these conditions is met once. Specifically with these two tests:

[TestMethod]
public void CanNotHaveNegativeServiceWindow()
{
    // arrange
    var request = new CreateCaseRequest
    {
        ServiceWindowStart = new DateTime(2014, 12, 31, 12, 00, 00),
        ServiceWindowEnd = new DateTime(2014, 12, 31, 11, 00, 00)
    };

    // act
    var result = request.GetValidationErrors();

    // assert
    Assert.AreEqual(1, result.Count());
}

[TestMethod]
public void CanHaveServiceWindow()
{
    // arrange
    var request = new CreateCaseRequest
    {
        ServiceWindowStart = new DateTime(2014, 12, 31, 11, 00, 00),
        ServiceWindowEnd = new DateTime(2014, 12, 31, 12, 00, 00)
    };

    // act
    var result = request.GetValidationErrors();

    // assert
    Assert.AreEqual(0, result.Count());
}

      

One test confirms a positive result for that particular condition if

, the other verifies a negative result. Which block is not covered? What boolean condition exists that I am missing?

+3


source to share


2 answers


When you compare values Nullable<T>

, the C # compiler generates additional checks to see if the values Nullable<T>

matter. These checks will always match your code, because you have already checked all the tags null

.

Changing the condition for



if (ServiceWindowStart.Value > ServiceWindowEnd.Value)

      

should fix this problem.

+3


source


Light beige doesn't mean the code hasn't been covered. This means that it was only partially covered. Yours ServiceWindowStart

and ServiceWindowEnd

are null. But you only check them with values. And you are not testing for equality. Adding a test for null values ​​and for the case when they are equal should cover the missing test cases.

Another possible reason for this result might be because code coverage is done in IL code, not C # code. And the IL equivalent cannot be completely covered, or the structure of the code cannot be preserved.

Optimizing the build can fix the problem. Go to Solution Explorer -> Properties -> Build tab -> Select the Optimize Code checkbox.



Run code analysis using this option.

Here's an interesting blog post that covers this topic: http://blogs.msdn.com/b/ddietric/archive/2009/10/21/all-the-wonderful-colors-of-code-coverage.aspx

+2


source







All Articles