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

07. Test Quotes Notifier

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: 06. Learn Mocking Unit Test Data Next episode: 08. Mock API Requests

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

Now in the setup method, we need to create a object of the MockQuotesService class and pass that object to the QuotesNotifier constructor.

setUp(() {
  mockQuotesService = MockQuotesService();
  sut_quotesNotifier = QuotesNotifier(mockQuotesService);
});
test('Check initial values are correct', () {
  // assert
  expect(sut_quotesNotifier.isLoading, false);
  expect(sut_quotesNotifier.quotes, []);
});
group('get quotes', () {
  // test cases goes here
});
test('getQuotes should call getQuotes function', () async {
  // Arrange
  when(() => mockQuotesService.getQuotes()).thenAnswer((_) async => []);  
  // Act
  await sut_quotesNotifier.getQuotes();
  // Assert
  verify(() => mockQuotesService.getQuotes()).called(1);
});
test('''Loading data indicator, 
  sets quotes, indicates data is not loaded anymore''', () async {
    // arrange
    when(() => mockQuotesService.getQuotes()).thenAnswer((_) async => [
        Quotes( 1,'Test Quote 1','Test Author 1'),
        Quotes(2, 'Test Quote 2','Test Author 2'),
        Quotes(3,'Test Quote 3','Test Author 3',)
      ]);
    final future = sut_quotesNotifier.getQuotes();  
    expect(sut_quotesNotifier.isLoading, true);
    await future;
    expect(sut_quotesNotifier.quotes, [
        Quotes( 1,'Test Quote 1','Test Author 1'),
        Quotes(2, 'Test Quote 2','Test Author 2'),
        Quotes(3,'Test Quote 3','Test Author 3',)
      ]);
    expect(sut_quotesNotifier.isLoading, false);
});
final mockQuotesForTesting = [
  Quotes(
    1,
    'Test Quote 1',
    'Test Author 1',
  ),
  Quotes(
    2,
    'Test Quote 2',
    'Test Author 2',
  ),
  Quotes(
    3,
    'Test Quote 3',
    'Test Author 3',
  ),
];
void arrageQuotesServiceReturnsQuotes() {
    when(() => mockQuotesService.getQuotes())
        .thenAnswer((_) async => mockQuotesForTesting);
}
class MockQuotesService extends Mock implements QuotesService {}

void main() {
  late QuotesNotifier sut_quotesNotifier;
  late MockQuotesService mockQuotesService;

  /// Initialise or set up everything needed for the test.
  /// runs everytime before each and every test
  setUp(() {
    mockQuotesService = MockQuotesService();
    sut_quotesNotifier = QuotesNotifier(mockQuotesService);
  });

  test('Should check initial values are correct', () {
    expect(sut_quotesNotifier.isLoading, false);
    expect(sut_quotesNotifier.quotes, []);
  });

  group('getQuotes', () {

    void arrageQuotesServiceReturnsQuotes() {
      when(() => mockQuotesService.getQuotes())
          .thenAnswer((_) async => mockQuotesForTesting);
    }

    test('Get Quotes using the QuotesService', () async {
      arrageQuotesServiceReturnsQuotes();
      await sut_quotesNotifier.getQuotes();
      verify(() => mockQuotesService.getQuotes()).called(1);
    });

    test('''Loading data indicator, 
      sets quotes, indicates data is not loaded anymore''', () async {
      arrageQuotesServiceReturnsQuotes();
      final future = sut_quotesNotifier.getQuotes();
      expect(sut_quotesNotifier.isLoading, true);
      await future;
      expect(sut_quotesNotifier.quotes, mockQuotesForTesting);
      expect(sut_quotesNotifier.isLoading, false);
    });
  });
}