Skip to content
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