Testing in Flutter

Sep 19 2023 · Dart 2.19.3, Flutter 3.7.6, Android Studio 2021.3.1, Visual Studio Code 1.7.4

Part 2: Write Unit Tests

06. Learn Mocking Unit Test Data

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Test Change Notifier Next episode: 07. Test Quotes Notifier

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Let’s learn about mocking. In the previous episodes, you learned about unit testing. You wrote your first unit test. You also learned about the concept of mocking. In this episode, you will learn how to mock data. Mock, in general, means creating fake data or class.

class MockQuotesService implements QuotesService {
  
}
@override  
Future<void> getQuotes() async {
  // Todo: adding a function which will return mock data   
}
return [
  Quotes(
    1,
    'Test Quote 1',
    'Test Author 1',
  ),
  Quotes(
    2,
    'Test Quote 2',
    'Test Author 2',
  ),
  Quotes(
    3,
    'Test Quote 3',
    'Test Author 3',
  ),
];
dev_dependencies:
  flutter_test:
    sdk: flutter
  mocktail: <latest_version>
class MockQuotesService extends Mock implements QuotesService {}
    late MockQuotesService mockQuotesService;