Is it possible to mock or fake a class that needs to be extended with some other class with mockito
plugin in Dart?
I have the following example of inheritance:
abstract class Animal {
Animal(this.emoji);
final String emoji;
String greet();
}
class Dog extends Animal {
Dog(super.emoji);
@override
String greet() {
return '$emoji: Woof!';
}
}
class Poodle extends Dog {
Poodle(super.emoji);
}
class FakePoodle extends Fake implements Poodle {
// Raises a runtime error:
// Runtime error: No associated positional super constructor parameter.
FakePoodle(super.emoji);
}
I want to mock the class FakePoodle
that would still inherit all the properties of the class Dog
, using a constructor and make it mock with mockito
at the same time.
Here are my tests:
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
void main() {
group('Dog tests', () {
test('should return a dog greet', () {
expect(Dog('').greet(), ': Woof!');
});
test('Poodle should return a poodle greet', () {
expect(Poodle('').greet(), ': Woof!');
});
test('Fake Poodle should greet only once', () {
final FakePoodle fakePoodle = FakePoodle('');
fakePoodle.greet();
verify(fakePoodle.greet());
});
});
}
I found the following FAQ section from mockito
plugin:~
How do I mock an extension method?
If there is no way to override some kind of function, then mockito cannot mock it. See the above answer for further explanation, and alternatives.
Is there a way I can write integration tests like this in a clean manner?