felangel / felangel/mocktail

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

Đang mở
#179 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Dart
Star
702
Fork
88
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.