felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
588 stars 80 forks source link

mocktail gives confusing errors when there is an incomplete mock #237

Closed erickzanardo closed 3 weeks ago

erickzanardo commented 2 months ago

Describe the bug

If you make an incomplete mock, like calling when but forgetting to call thenReturn or thenAnswer, when you start a new mock, mocktail will break on that line, making you think that the issue is there, when it actually isn't, making it hard to track the error.

To Reproduce

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

class _MockFoo extends Mock implements Foo {}

class Foo {
  const Foo({
    required Map<String, dynamic> data,
  }) : _data = data;
  final Map<String, dynamic> _data;

  dynamic operator [](String key) => _data[key];

  String sayHello() => 'Hello, ${_data['Name']}!';
}

void main() {

  late Foo foo;

  setUp(() {
    foo = _MockFoo();
    when(() => foo['Name']); // <- This is incomplete
  });

  test('say hello, says hello', () {
    when(() => foo.sayHello()).thenReturn('Hello, Bond, James Bond!'); // <- Test will break here, but this mock is fine
    expect(foo.sayHello(), 'Hello, Bond, James Bond!');
  });
}

Expected behavior I understand that it might be hard to break in the incomplete mock, but maybe the messaging could at least be improved, since in a big test file, tracking down this type of error can be quite challenging.

felangel commented 2 months ago

Seems like a duplicate of #213

felangel commented 3 weeks ago

Closing in favor of #213