Skip to content

Generate spy mockΒ #575

Open
Open
@JulianBissekkou

Description

@JulianBissekkou

Spy was removed when we migrated to null safety. Spy also used the mirrors API which introduces problems on some platforms.

Are we able to implement the Spy feature using code gen? A potential spy implementation could be another layer ontop of a mock that proxies the calls to an instance that can be provided when creating the spy object.

Here is some pseudo code to show you how this feature can look like.

@GenerateMocks([Cat])
// This will generate the "SpyCat" class.
// We can also add `final List<Type> spyClasses` to the `GenerateMocks` annotation.
@SpyProxyMock([Cat])
void main() {
  test(
    "Example test",
    () {
      final spy = SpyCat(realInstance: Cat("liam"));
      when(spy.eat(any)).thenAnswer((_) => print("..."));
    },
  );
}

In noSuchMethod we can then decide if we have to call the realInstance or if we execute a mocked invocation.

I would love to contribute to this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestcontributions-welcomeContributions welcome to help resolve this (the resolution is expected to be clear from the issue)type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions