简体   繁体   中英

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#.

Here is the handle function which SonarQube says is not covered by tests在此处输入图像描述

But this function is tested by these two unit tests:

[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?

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. This caused the tests to not execute properly in the pipeline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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