Skip to content

Add a few mocking utils #75

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/netglade_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased
- Require Dart SDK 3.8.0 or later.
- Add `verifyCalledOnceAndNoMoreInteractions`
- Add shortcut calls for stubbing When with Option result.

## 2.5.0
- Dependencies update.
Expand Down
25 changes: 21 additions & 4 deletions packages/netglade_utils/lib/src/testing/mock_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,37 @@ extension VerificationResultEx on VerificationResult {
void calledOnce() => called(1);
}

/// Verifies that the given mock was called exactly once with the provided function.
///
/// Also verifies that no other interactions with the mock occurred.
// ignore: prefer-static-class, prefer-typedefs-for-callbacks, avoid-dynamic, keep it.
void verifyCalledOnceAndNoMoreInteractions<T>(dynamic mock, T Function() verifyCall) {
verify(verifyCall).calledOnce();
verifyNoMoreInteractions(mock);
}

extension SuccessAnswer<T, E> on When<Future<Result<T, E>>> {
void thenAnswerWithSuccess(T value) => thenAnswer((_) async => Success(value));

void thenAnswerWithError(E error) => thenAnswer((_) async => Error(error));
}

extension SuccessAnswerOr<T, E> on When<FutureOr<Result<T, E>>> {
void thenAnswerWithSuccess(T value) => thenAnswer((_) => Success(value));

void thenAnswerWithError(E error) => thenAnswer((_) => Error(error));
}

extension ErrorAnswer<T, E> on When<Future<Result<T, E>>> {
void thenAnswerWithError(E error) => thenAnswer((_) async => Error(error));
extension OptionAnswer<T> on When<Future<Option<T>>> {
void thenAnswerWithNone() => thenAnswer((_) async => const None());

void thenAnswerWithSome(T value) => thenAnswer((_) async => Some(value));
}

extension ErrorAnswerOr<T, E> on When<FutureOr<Result<T, E>>> {
void thenAnswerWithError(E error) => thenAnswer((_) => Error(error));
extension OptionAnswerOr<T> on When<FutureOr<Option<T>>> {
void thenAnswerWithNone() => thenAnswer((_) async => const None());

void thenAnswerWithSome(T value) => thenAnswer((_) async => Some(value));
}

extension StreamSubscriptionAnswer<T> on When<StreamSubscription<T>> {
Expand Down