felangel / felangel/mocktail

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

Abierto
#179 1 comentario 1 reacción 0 asignados Ver en GitHub
bug
Lenguaje dominante
Dart
Estrellas
702
Forks
88
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

*Mocktail 0.3.0*

```dart
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
});
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.