Skip to content

Commit

Permalink
fix(mocktail): verify use argmatcher from invocation when matcher is …
Browse files Browse the repository at this point in the history
…not provided (#27)
  • Loading branch information
felangel authored Mar 8, 2021
1 parent 251a71d commit ba5fd66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/mocktail/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.0.2-dev.5

- fix: `verify` should use arg matcher from invocation if a matcher wasn't provided

# 0.0.2-dev.4

- **BREAKING** fix: `verifyInOrder` handles multiple method invocation verifications
Expand Down
18 changes: 15 additions & 3 deletions packages/mocktail/lib/src/_is_invocation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,21 @@ class _InvocationForMatchedArguments extends Invocation {
while (storedIndex < _storedArgs.length &&
positionalIndex < invocation.positionalArguments.length) {
final arg = _storedArgs[storedIndex];
positionalArguments.add(arg);
storedIndex++;
positionalIndex++;
final dynamic positionalArgument =
invocation.positionalArguments[positionalIndex];
if (positionalArgument == null ||
positionalArgument == arg._fallbackValue) {
// Add the [ArgMatcher] given to the argument matching helper.
positionalArguments.add(arg);
storedIndex++;
positionalIndex++;
} else {
// An argument matching helper was not used; add the [ArgMatcher] from
// [invocation].
positionalArguments
.add(invocation.positionalArguments[positionalIndex]);
positionalIndex++;
}
}
while (positionalIndex < invocation.positionalArguments.length) {
// Some trailing non-ArgMatcher arguments.
Expand Down
2 changes: 1 addition & 1 deletion packages/mocktail/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mocktail
description: A Dart mocking library which simplifies mocking with null safety support and no manual mocks or code generation.
version: 0.0.2-dev.4
version: 0.0.2-dev.5
repository: https://github.com/felangel/mocktail
homepage: https://github.com/felangel/mocktail/tree/main/packages/mocktail

Expand Down
7 changes: 7 additions & 0 deletions packages/mocktail/test/mocktail_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Foo {
void voidWithDefaultNamedArg({int x = 0}) {}
void voidWithDefaultNamedArgs({int x = 0, int y = 0}) {}
void voidWithPositionalAndOptionalNamedArg(int x, {int? y}) {}
void voidWithPositionalArgs(int x, int y) {}
}

class Bar {
Expand Down Expand Up @@ -413,6 +414,12 @@ void main() {
);
});

test('when voidWithPositionalArgs with partial matchers', () {
when(() => foo.voidWithPositionalArgs(any(), any())).thenReturn(null);
expect(() => foo.voidWithPositionalArgs(1, 10), returnsNormally);
verify(() => foo.voidWithPositionalArgs(1, any())).called(1);
});

test('throws Exception when thenThrow is used to stub the mock', () {
final exception = Exception('oops');
when(() => foo.streamValue).thenThrow(exception);
Expand Down

0 comments on commit ba5fd66

Please sign in to comment.