JasonBock / Rocks

A mocking library based on the Compiler APIs (Roslyn + Mocks)
MIT License
237 stars 20 forks source link

Mocks With Generic Methods and Events Cannot Be Mocked #295

Closed JasonBock closed 8 months ago

JasonBock commented 8 months ago

To Reproduce

Run this in a test:

var code =
    """
    using Rocks;
    using System;

    [assembly: RockCreate<IGenericsAndEvent>]

    public interface IGenericsAndEvent
    {
        int Work<T>(T value);
        int ConstrainedWork<T>(T value) where T : class, new();
        event EventHandler MyEvent;
    }
    """;

Expected behavior A mock is created:

Actual behavior Errors occur like this:

error CS0246: The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

Additional context The issue is that the event extensions do not define the generics on the methods, which causes the issue. Probably also need to bring along the constraints on those methods as well.

This was found on a number of types, Blazored.LocalStorage.ILocalStorageService being one of them. This is related to the recent code generation changes in 8.0.0.