简体   繁体   English

尽管测试通过代码运行,但 ReSharper dotcover 仍显示 0% 的覆盖率

[英]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%.我假设这是我的错误,但我不明白为什么 ReSharper dotcover 将我对某些查询(和命令)的测试覆盖率显示为 0%。

So I have a .NET Core CQRS API that is made up of a lot of EF Core LINQ.所以我有一个由很多 EF Core LINQ 组成的 .NET Core CQRS API。 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):下面是我的查询的主要执行方法之一的简单示例(我省略了 DI 构造函数,但我确定您知道这个想法):

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.测试通过(如果我破坏 LINQ 中的逻辑也会失败)或代码中的任何其他逻辑。

However, running dotcover runs the test, passes it but says that none of it is covered.然而,运行 dotcover 会运行测试,通过它但说它没有被覆盖。

I would love to know why because it's really driving me insane and worries me that I've done something completely wrong!我很想知道为什么,因为这真的让我发疯,让我担心我做错了什么!

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:在尝试将日志提交给 JetBrains 时,我执行了以下操作:

  1. In ReSharper |在 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.然后我重新运行 dotcover,突然我所有的测试覆盖都显示出来了,我所有的测试覆盖代码高亮显示都正确显示。

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.我已经给 JetBrains 发回了一条消息,如果他们给我任何关于为什么解决这个问题的信息,我也会发布。

I had a similar issue when dotCover didn't recognize some of the unit tests.当 dotCover 无法识别某些单元测试时,我遇到了类似的问题。

I was able to resolve it by removing Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll from Test Project references and installing MSTest.TestFramework and MSTest.TestAdapter nuget packages.我能够通过从测试项目引用中删除Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll并安装MSTest.TestFrameworkMSTest.TestAdapter nuget 包来解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM