Skip to content

Verify positional parameter 'null' not working together with any() #256

@moderateroni

Description

@moderateroni

Describe the bug
When you have multiple positional parameters of the same (nullable) Type in your mocked Method, you cannot verify that one positional parameter is null and others are any().

To Reproduce
Steps to reproduce the behavior:

This Test will fail, but from my point of view it should not:

import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockListener extends Mock {
  void call(int? previous, int next);
}

void main() {
  late MockListener listener;

  setUp(() {
    listener = MockListener();
    registerFallbackValue(1);
  });

  test('verify listener is called with any()', () {
    // Arrange
    final previousInt = null;
    final nextInt = 3;

    // Act
    listener(previousInt, nextInt);

    // Assert
    verify(() => listener(null, any())).called(1);
  });
}

Expected behavior
Tests like this should not fail

Additional context
In _is_invocation.dart, there is this line of code in _reconstitutePositionalArgs:
if (positionalArgument == null || positionalArgument == arg._fallbackValue)

shouldn't the first check be removed? i did this and all tests are still successful.

easy workaround of course is to also use a matcher for the first positional argument, so
verify(() => listener(any(that: isNull), any())).called(1);
will work

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions