mockito
mockito copied to clipboard
Mocking XFile sometimes fails; mockito seems to generate inconsistent imports
Based on a report from StackOverflow: https://stackoverflow.com/q/71830050/
Given code:
import 'package:cross_file/cross_file.dart';
import 'package:mockito/annotations.dart';
import 'xfile_test.mocks.dart';
class Cat {}
@GenerateMocks([XFile])
void main() {
var mockXFile = MockXFile();
print(mockXFile is XFile);
}
Then running:
$ flutter pub run build_runner build
$ flutter test test/xfile_test.dart
false
No tests ran.
No tests were found.
If I change the @GenerateMocks line to @GenerateMocks([XFile, Cat]) and repeat, it works:
true
No tests ran.
No tests were found.
I compared the generated xfile_test.mocks.dart files, and the broken version generated an import:
import 'package:cross_file/src/types/interface.dart' as _i2;
whereas the working version generated an import:
import 'package:cross_file/cross_file.dart' as _i2;
What's going on?
I'm using mockito 5.1.0 and source_gen 1.2.1.
Sounds like it could be a duplicate of https://github.com/dart-lang/mockito/issues/525
I also was confused why MockXFile is XFile is true for one import and not the other since XFile is defined for both paths. On closer inspection, they're separate XFile classes: the XFile class defined in package:cross_file/src/types/interface.dart is a stub implementation, but the intended XFile comes from a conditional export for dart.library.html vs dart.library.io. Maybe that conditional export is confusing the code generator?
Hm... right, I think the reason is conditional export... but a bit surprised we even followed the import/export chain, I'd expect it to always stop at 'package:cross_file/cross_file.dart'.