Jason
Jason

Reputation: 113

Unit tests not showing code coverage with async/await

I'm using vs2017 and none of my unit tests are showing code coverage. I"m using async/await and MOQ.

[TestMethod]
[TestCategory("SeriesRepository")]
public void GetSeriesAsyncShouldReturnSeriesId12345()
{
    var repositoryMock = new Mock<ISeriesRepository>();

    var seriesId = "12345";
    var channel = 75864;            

    var mockSeries = new Mock<ISeries>();
    mockSeries.SetupGet(x => x.SeriesId).Returns("12345");

    repositoryMock
        .Setup(x => x.GetSeriesAsync(seriesId, channel))
        .ReturnsAsync(mockSeries.Object);

    var result = repositoryMock.Object.GetSeriesAsync(seriesId, channel).Result;

    result.SeriesId.Should().Be(seriesId);
}

Upvotes: 1

Views: 2286

Answers (2)

Nkosi
Nkosi

Reputation: 247078

First fact is that you are not actually testing anything in your test as you simply create mocks and invoke the mocks.

You are simply testing that the mocking framework works as advertised.

Secondly the tests can be made async as well to allow the test to exercise sequentially.

[TestMethod]
[TestCategory("SeriesRepository")]
public async Task GetSeriesAsyncShouldReturnSeriesId12345() {
    var repositoryMock = new Mock<ISeriesRepository>();

    var seriesId = "12345";
    var channel = 75864;            

    var mockSeries = new Mock<ISeries>();
    mockSeries.Setup(_ => _.SeriesId).Returns(seriesId);

    repositoryMock
        .Setup(_ => _.GetSeriesAsync(seriesId, channel))
        .ReturnsAsync(mockSeries.Object);

    var result = await repositoryMock.Object.GetSeriesAsync(seriesId, channel);

    result.SeriesId.Should().Be(seriesId);
}

What is suppose to happen is that you mock the dependencies of a target under test to behave as expected in order to verify some desired behavior

Lets say we wanted to test a target method of a class. Something like

public class SeriesService {
    private readonly ISeriesRepository repository;

    public SeriesService(ISeriesRepository repository
        this.repository = repository;
    }

    public async Task<ISeries> GetSeries(string seriesId, int channel) {
        var series = await repository.GetSeriesAsync(seriesId, channel);

        //...do some other stuff

        return series;
    }
}

An example test would then look like this

[TestMethod]
[TestCategory("SeriesRepository")]
public async Task TargetMethodShouldReturnSeriesId12345() {
    //Assert
    var repositoryMock = new Mock<ISeriesRepository>();

    var seriesId = "12345";
    var channel = 75864;            

    var mockSeries = new Mock<ISeries>();
    mockSeries.Setup(_ => _.SeriesId).Returns(seriesId);

    repositoryMock
        .Setup(_ => _.GetSeriesAsync(seriesId, channel))
        .ReturnsAsync(mockSeries.Object);

    var target = new SeriesService(repositoryMock.Object);

    //Act
    var result = await target.GetSeries(seriesId, channel);

    //Assert
    result.Should().NotBeNull();
    result.SeriesId.Should().Be(seriesId);
}

Upvotes: 1

Shankar
Shankar

Reputation: 18

There isn't any wait for your repositoryMock setup to complete. So, your test case is ending before your async calls are complete.

Make sure you wait by adding:

repositryMock.setup(...).ReturnsAsync(...).GetAwaiter().GetResult();

Or

repositryMock.setup(...).ReturnsAsync(...).Result;

Or

repositryMock.setup(...).ReturnsAsync(...).Wait();

This should solve your problem.

Upvotes: 0

Related Questions