Member-only story
Easy Mocking With Mocktail
2 min readSep 3, 2021
Rather than mess around with code generation just to mock tests, why not let me show you how to use mocktail to mock in tests.
Installing Mocktail
To install Mocktail in your pubspec enter:
dev_dependencies:
mocktail: ^0.1.4
And since there are no other settings to set, let’s get to implementing mocks.
Implementing A Mock
So we are back at the widget test dart file again:
class MyCounter {int initCount = 0;int? incrementMe() => initCount++;}class MockMyCounter extends Mock implements MyCounter {}It's just a simple create a fake class, then mock the class. Then we need to in the root of the main function create a reference:
late MyCounter myCounter;
// ignore: no-empty-block
setUpAll(() async {
TestWidgetsFlutterBinding.ensureInitialized();
myCounter = MockMyCounter();
});
So now that we have our dependency injected into our setup method, we can now use it in a test block: