Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default, common use stubs #77

Closed
erickzanardo opened this issue Sep 13, 2021 · 4 comments
Closed

Default, common use stubs #77

erickzanardo opened this issue Sep 13, 2021 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@erickzanardo
Copy link

Mocktail could provide some default stub instances that are common use and not project specific.

A common one that I usually end up writing on every project is a VoidCallbackStub, usually to check if a onPressed callback was called on widget testing.

An implementation suggestion:

class _VoidCallbackStub {
  void onCall() {}
}

class VoidCallbackStub extends Mock implements _VoidCallbackStub {}

then we could use it like the following:

      final listener = VoidCallbackStub();
      await _pumpWidget(
        tester,
        CustomButton(
          label: 'label',
          onPressed: listener.onCall,
        ),
      );

      await tester.tap(find.byType(GestureDetector));
      await tester.pumpAndSettle();
   
      verify(listener.onCall).called(1);

Is this something worth adding to Mocktail?

@felangel
Copy link
Owner

felangel commented Oct 8, 2021

Hey @erickzanardo 👋
Thanks for opening an issue!

After some discussions, I think it would be best if we kept package:mocktail as a package which contains the core mocking APIs and for convenience APIs like this we could create a separate package. What do you think?

Also, for scenarios like the one you described it might be easier to forego using mocktail completely like:

var onPressedCallCount = 0;
await tester.pumpWidget(
  CustomButton(onPressed: () => onPressedCallCount++),
);
await tester.tap(...)
expect(onPressedCallCount, equals(1));

Let me know what you think and sorry for the delayed reply!

@felangel felangel self-assigned this Oct 8, 2021
@felangel felangel added question Further information is requested waiting for response Waiting for follow up labels Oct 8, 2021
@erickzanardo
Copy link
Author

Hey, thanks for the response.

Yeah, the counter variable is something that I also used and it work perfectly, but this proposal makes it a little bit more convenient.

A separate package sounds good too! Would that be something that could be kept under this mono repo? If so I would love to do a PR including it.

Otherwise if you prefer to make it external, I could maintain it.

@felangel
Copy link
Owner

felangel commented Feb 1, 2022

Sorry for the delayed response! I'd prefer to create a separate package (maybe https://pub.dev/packages/mocktailx would accept a PR?).

Let me know what you think 👍

@erickzanardo
Copy link
Author

Sounds good! I will work on this on a external package, thanks @felangel !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants