简体   繁体   English

SonarQube 指出“未涵盖的行”,但单元测试涵盖了它们(MediatR 句柄)

[英]SonarQube states "lines not covered" but they are covered by UnitTests (MediatR Handle)

I'm facing a problem with SonarQube and I think it has to do with the Handle function of MediatR in C#.我遇到了 SonarQube 的问题,我认为这与 C# 中 MediatR 的句柄 function 有关。

Here is the handle function which SonarQube says is not covered by tests这是句柄 function SonarQube 表示未被测试覆盖在此处输入图像描述

But this function is tested by these two unit tests:但是这个 function 是通过这两个单元测试来测试的:

[Fact]
public async Task SpeichereBetriebCommandHandler_SollteGuidZurueckgeben_WennBetriebNichtExistiert()
{
    // Arrange
    Fixture fixture = new Fixture();

    var speichereBetriebCommand = fixture.Build<SpeichereBetriebCommand>().Create();
    
    // Act
    var result = await _speichereBetriebCommandHandler.Handle(speichereBetriebCommand, new System.Threading.CancellationToken());
    
    // Assert
    result.Should().NotBe(String.Empty);
}

[Fact]
public async Task SpeichereBetriebCommandHandler_SollteLeerStringZurueckgeben_WennBetriebExistiert()
{
    // Arrange
    Fixture fixture = new Fixture();

    var someData .....

    var betrieb = fixture.Build<BetriebDto>()
        .With(b => b.SomeData, someData )
        .
        .
        .
        .Create();

    var speichereBetriebCommand = fixture.Build<SpeichereBetriebCommand>()
        .With(b => b.SomeData, someData)
        .
        .
        .
        .Create();
    
    await _betriebRepository.AddAsync(betrieb);
    await _statistikContextRepository.SaveChangesAsync();
    
    // Act
    var result = await _speichereBetriebCommandHandler.Handle(speichereBetriebCommand, new System.Threading.CancellationToken());
    
    // Assert
    result.Should().Be(String.Empty);
}

I've also debugged the unit tests and both land in the handle function. So I've tested the complete function. Why does SonarQube not agree with me?我还调试了单元测试,两者都落在句柄 function 中。所以我测试了完整的 function。为什么 SonarQube 不同意我的看法?

The issue has been resolved.问题已解决。 The root cause was that I was utilizing XUnit, but there were still some NUnit references present in my.csproj file.根本原因是我正在使用 XUnit,但 my.csproj 文件中仍然存在一些 NUnit 引用。 This caused the tests to not execute properly in the pipeline.这导致测试无法在管道中正确执行。

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

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