Completed
Last Updated: 12 May 2022 07:51 by ADMIN
Mihail
Created on: 23 Sep 2021 14:24
Type: Bug Report
4
System.InvalidProgramException is thrown when unit testing an implementation of Microsoft.Extensions.Caching.Distributed.IDistributedCache with enabled code coverage
A System.InvalidProgramException is thrown when async unit tests are executed inside Visual Studio with code coverage. The unit tests are testing an implementation of Microsoft.Extensions.Caching.Distributed.IDistributedCache.
2 comments
ADMIN
Mihail
Posted on: 12 May 2022 07:51

The issue is fixed and available with R2 2022 version 2022.2.511.1

Regards,
Mihail
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

ADMIN
Ivo
Posted on: 12 Nov 2021 09:51

*** An update to the original content ***

The issue is also reproducible with a more generic case and occurs with version 16.10.0 and above. Here is the sample code:

public class Class1
{
    public async Task<int> EchoAsync(int v)
    {
        return await Task.FromResult(v);
    }
}

...

[TestMethod]
public async Task TestMethodWithIssue()
{
    var sut = Mock.Create<Class1>();

    Mock.Arrange(() => sut.EchoAsync(Arg.AnyInt))
        .Returns(
            async (int v) =>
                {
                    await Task.Delay(300);
                    return await Task.FromResult(v * 2);
                });

    var result = await sut.EchoAsync(10);

    Mock.Assert(sut);
    Assert.AreEqual(20, result);
}

The only possible workaround till now is to disable automatic Mock.Reset performed by the profiler during instrumentation:

[TestMethod]
[DisableAutomaticRepositoryReset(AllowMocking = true)]
public async Task TestMethodWithIssueWorkarund()
{
    try
    {
        var sut = Mock.Create<Class1>();

        Mock.Arrange(() => sut.EchoAsync(Arg.AnyInt))
            .Returns(
                async (int v) =>
                    {
                        await Task.Delay(300);
                        return await Task.FromResult(v * 2);
                    });

        var result = await sut.EchoAsync(10);

        Mock.Assert(sut);
        Assert.AreEqual(20, result);
    }
    finally
    {
        Mock.Reset();
    }
}

 

 

Regards,
Ivo
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.