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

Confusing failure with nested mocks #204

Open
eseidel opened this issue Jul 26, 2023 · 6 comments
Open

Confusing failure with nested mocks #204

eseidel opened this issue Jul 26, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@eseidel
Copy link

eseidel commented Jul 26, 2023

I meant to file this a few weeks ago, sorry.

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

class One {
  final Two two = Two();
}

class Two {
  final String three = '';
}

class _MockOne extends Mock implements One {}

class _MockTwo extends Mock implements Two {}

void main() {
  test('nested mocks', () {
    final one = _MockOne();
    final two = _MockTwo();
    when(() => one.two).thenReturn(two);
    when(() => one.two.three).thenReturn('three');
    expect(one.two.three, 'three');
  });
}
00:02 +0 -1: nested mocks [E]                                                                                                         
  type 'String' is not a subtype of type 'Two'
  test/foo_test.dart 12:7   _MockOne.two
  test/foo_test.dart 22:16  main.<fn>

That's a better error than I remember (I remember it being silent?) But it's still pretty confusing. Feel free to close.

@felangel felangel self-assigned this Jul 26, 2023
@felangel felangel added the enhancement New feature or request label Jul 26, 2023
@eseidel
Copy link
Author

eseidel commented Jul 26, 2023

This was the other bug I thought I had, but can't seem to repro now:

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

class One {
  final Two two = Two();
}

class Two {
  String three(String arg) => arg;
}

class _MockOne extends Mock implements One {}

class _MockTwo extends Mock implements Two {}

void main() {
  test('nested mocks', () {
    final one = _MockOne();
    final two = _MockTwo();
    when(() => one.two).thenReturn(two);
    when(() => two.three('three')).thenReturn('three');
    one.two.three('three');
    one.two.three('three');
    verify(() => one.two.three('three')).called(2);
  });
}

Where call counts would get double-counted when using nested mocks in the verify() call.

@felangel
Copy link
Owner

This was the other bug I thought I had, but can't seem to repro now:

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

class One {
  final Two two = Two();
}

class Two {
  String three(String arg) => arg;
}

class _MockOne extends Mock implements One {}

class _MockTwo extends Mock implements Two {}

void main() {
  test('nested mocks', () {
    final one = _MockOne();
    final two = _MockTwo();
    when(() => one.two).thenReturn(two);
    when(() => two.three('three')).thenReturn('three');
    one.two.three('three');
    one.two.three('three');
    verify(() => one.two.three('three')).called(2);
  });
}

Where call counts would get double-counted when using nested mocks in the verify() call.

Hmm let me know if you find a way to reproduce it and I’ll check it out 👀

@eseidel
Copy link
Author

eseidel commented Jul 26, 2023

I suspect I have the curse of knowledge now and just know to avoid these things. :) Thanks!

@bryanoltman
Copy link

I had a test succeed when it shouldn't have, I think due to this issue. To reproduce:

code (abbreviated for clarity):

  final deleteReleaseQuery = // the query;
  final deleteReleaseResponse = await bigqueryClient.query(deleteReleaseQuery);

    final deletePatchesQuery = '''
lkajsdfjasdkfjklasdfjklsdajfklasdjklasdfjklasdjf''';
    final deletePatchesResponse =
        await bigqueryClient.query(deletePatchesQuery);

test:

        when(() => bigqueryClient.projectId).thenReturn('test-project-id');
        verifyInOrder([
          () => bigqueryClient.query('''
DELETE `${bigqueryClient.projectId}.command_metadata.release`
WHERE id = ${release.id};'''),
          () => bigqueryClient.query(
                '''
DELETE `${bigqueryClient.projectId}.command_metadata.patch`
WHERE id in (11);''',
              ),
        ]);

This test succeeded because I was referencing the mocked bigqueryClient.projectId in verifyInOrder.

@felangel
Copy link
Owner

felangel commented Apr 1, 2024

Thanks @bryanoltman!

@felangel
Copy link
Owner

Also seems related to #179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants