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

verify verifies against wrong class when a second mock is called within the same verify call #179

Open
Clavum opened this issue Jan 22, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@Clavum
Copy link

Clavum commented Jan 22, 2023

This issue is best explained and demonstrated in code. There is a comment that explains the issue.

Mocktail 0.3.0

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

class SomeDataProvider {
  String getValue() => 'value';
}

class SomeClassToMockAndVerify {
  void doSomethingImportant(String value) {}
}

class SubjectUnderTest {
  final SomeDataProvider _someDataProvider;
  final SomeClassToMockAndVerify _someClassToMockAndVerify;

  SubjectUnderTest(this._someDataProvider, this._someClassToMockAndVerify);

  void methodUnderTest() {
    // You'll see what this is for later:
    _someDataProvider.getValue();
    _someDataProvider.getValue();
    _someDataProvider.getValue();
    _someDataProvider.getValue();
    _someDataProvider.getValue();

    _someClassToMockAndVerify.doSomethingImportant(
      _someDataProvider.getValue(),
    );
  }
}

class MockSomeDataProvider extends Mock implements SomeDataProvider {}

class MockSomeClassToMockAndVerify extends Mock implements SomeClassToMockAndVerify {}

void main() {
  test('bug example', () {
    final mockSomeDataProvider = MockSomeDataProvider();
    when(() => mockSomeDataProvider.getValue()).thenReturn('mock value');
    final mockSomeClassToMockAndVerify = MockSomeClassToMockAndVerify();

    final subjectUnderTest = SubjectUnderTest(
      mockSomeDataProvider,
      mockSomeClassToMockAndVerify,
    );
    subjectUnderTest.methodUnderTest();

    verify(
      () => mockSomeClassToMockAndVerify.doSomethingImportant(
        // Whoops! Here we accidentally call a method of a *second* mock during a verify call. Now,
        // the verification is broken, and is verifying `mockSomeDataProvider`'s calls instead of
        // `mockSomeClassToMockAndVerify`'s calls!
        // As evidence of this, the test fails saying there were 6 calls instead of 1.
        // This is very confusing as a developer, since we're verifying `doSomethingImportant` and
        // being told it was called 6 times when it was only called once.
        // Of course, we should use the literal 'mock value' string here, but my ask is just that
        // this scenario be better handled, perhaps with a special exception.
        mockSomeDataProvider.getValue(),
      ),
    ).called(1);
    // Expected: <1>
    // Actual: <6>
    // Unexpected number of calls
  });
}
@Clavum Clavum changed the title verify verifies against wrong class when a second mock is called within a verify call verify verifies against wrong class when a second mock is called within the same verify call Jan 22, 2023
@felangel felangel added the bug Something isn't working label Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants