Skip to content

[Bug]: Mocking interface IRequestAdapter with constrained generic type parameter throws error #6456

Description

@joachimdekker

Description

When mocking IRequestAdapter from Microsoft.Kiota.Abstractions using IRequestAdapter.Mock(), tests won't build because the generated mock does not implement an interface member with a constrained generic type parameter.

This also occurs when using the [assembly: GenerateMock(typeof(IRequestAdapter))]

Expected Behavior

I expected the IRequestAdapter to be mocked when using both methods.

Actual Behavior

The build fails with the following output:

2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,110): Error CS0539 : 'IRequestAdapterMock.SendAsync<ModelType>(RequestInformation, ParsableFactory<ModelType>, Dictionary<string, ParsableFactory<IParsable>>?, CancellationToken)' in explicit interface declaration is not found among members of the interface that can be implemented
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(7,127): Error CS0535 : 'IRequestAdapterMock' does not implement interface member 'IRequestAdapter.SendAsync<ModelType>(RequestInformation, ParsableFactory<ModelType>, Dictionary<string, ParsableFactory<IParsable>>?, CancellationToken)'
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,110): Error CS0453 : The type 'ModelType' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,278): Error CS0314 : The type 'ModelType' cannot be used as type parameter 'T' in the generic type or method 'ParsableFactory<T>'. There is no boxing conversion or type parameter conversion from 'ModelType' to 'Microsoft.Kiota.Abstractions.Serialization.IParsable'.

Steps to Reproduce

  1. Import Nuget Package Microsoft.Kiota.Abstractions.
  2. Create a test class using the IRequestAdapter.
  3. Create a test that mocks the interface using Mock().
  4. Build the project (creating a full test isn't necessary).
using Microsoft.Kiota.Abstractions;

namespace TestSpace;

public class TestService(IRequestAdapter requestAdapter)
{
    public Task TestMethod()
    {
        RequestInformation requestInfo = new();
        return requestAdapter.SendNoContentAsync(requestInfo);
    }
}

public class Test
{
    [Test]
    public async Task TestMethod_Succeeds()
    {
        var requestAdapter = IRequestAdapter.Mock();
        requestAdapter.SendNoContentAsync(AnyArgs()).ReturnsAsync(Task.CompletedTask);
        var sut = new TestService(requestAdapter);
        
        await sut.TestMethod();

        requestAdapter.SendNoContentAsync(AnyArgs()).WasCalled(Times.Once);
    }
}

TUnit Version

1.61.15

.NET Version

.NET 10.0

Operating System

Windows

IDE / Test Runner

JetBrains Rider

Error Output / Stack Trace

2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,110): Error CS0539 : 'IRequestAdapterMock.SendAsync<ModelType>(RequestInformation, ParsableFactory<ModelType>, Dictionary<string, ParsableFactory<IParsable>>?, CancellationToken)' in explicit interface declaration is not found among members of the interface that can be implemented
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(7,127): Error CS0535 : 'IRequestAdapterMock' does not implement interface member 'IRequestAdapter.SendAsync<ModelType>(RequestInformation, ParsableFactory<ModelType>, Dictionary<string, ParsableFactory<IParsable>>?, CancellationToken)'
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,110): Error CS0453 : The type 'ModelType' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'
2>Microsoft_Kiota_Abstractions_IRequestAdapter_Mock.g.cs(18,278): Error CS0314 : The type 'ModelType' cannot be used as type parameter 'T' in the generic type or method 'ParsableFactory<T>'. There is no boxing conversion or type parameter conversion from 'ModelType' to 'Microsoft.Kiota.Abstractions.Serialization.IParsable'.

Additional Context

We are using this in the context of mocking the Graph Service to test our service that uses Graph for various applications.

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions