Search code examples
c#unit-testing.net-coredotcover

ReSharper dotcover showing 0% coverage despite tests running through code


I'm assuming this is an error on my part but I can't figure out why ReSharper dotcover is showing my test coverage of certain queries (and commands too) as 0%.

So I have a .NET Core CQRS API that is made up of a lot of EF Core LINQ. Below is a simple example of one of my queries's main execute method (I left out the DI constructor but I'm sure you git the idea):

public bool Execute(SelectIsReportRequested query)
{
     var context = _clientDatabase.GetContext(query.DatabaseId);

     var result = (from a in context.Assessments
                   join r in context.Registrations on a.AssessmentId equals r.AssessmentId
                   where a.PublicId == query.ResponseId
                   select r.ReportRequested).SingleOrDefault();

     return result == 1;
}

Then I have the following test that mocks the various bits and runs the query:

[TestMethod]
public void It_should_return_true_if_a_report_has_been_requested_for_the_givenassessment()
{
    const int assessmentId = 1;
    var responseId = Guid.NewGuid();
    var mockRepository = new Mock<ICViewClientContext>();

    var assessments = new List<Assessments>
    {
        new Assessments { AssessmentId = assessmentId, PublicId = responseId },
    };
     var registrations = new List<Registrations>
     {
        new Registrations { AssessmentId = assessmentId, ReportRequested = 1 },
     };

     mockRepository.Setup(x => x.Registrations).Returns(registrations.AsDbSetMock().Object);
     mockRepository.Setup(x => x.Assessments).Returns(assessments.AsDbSetMock().Object);

     var mockClientDatabase = new Mock<IClientDatabase>();
     mockClientDatabase.Setup(x => x.GetContext(1)).Returns(mockRepository.Object);

     var query = new Queries.Assessments.SelectIsReportRequested(2, responseId);
     var handler = new Queries.Assessments.SelectIsReportRequestedHandler(mockClientDatabase.Object);
     var result = handler.Execute(query);

     Assert.AreEqual(true, result);
 }

The tests passes (and will also fail if I break the logic in the LINQ) or any other logic in the code.

However, running dotcover runs the test, passes it but says that none of it is covered.

I would love to know why because it's really driving me insane and worries me that I've done something completely wrong!


Solution

  • So I think through blind luck I have been able to solve my issue and wanted to post what I did just in case it helps anyone else.

    Whilst trying to get the logs to submit to JetBrains I did the following:

    1. In ReSharper | Options… | dotCover | General, disabled 'Use preloaded Unit Test runners'
    2. Saved settings
    3. Went back and enabled 'Use preloaded Unit Test runners'
    4. Saved settings

    Then I re-ran dotcover and suddenly all my test coverage was shown and all my test cover code highlighting was shown correctly.

    I've sent a message back to JetBrains and if they give me any info as to why that solved it I'll post that too.