Widget Testing

Widget testing is otherwise called component testing. As its name proposes, it is utilized for testing a single widget, and the objective of this test is to check whether the widget works and looks true to form.

Widget testing proves to be useful when we are trying the particular widgets. On the off chance that the Text widget doesn’t contain the text, at that point, Widget testing throws an error expressing that the Text widget doesn’t have a text inside it. We likewise don’t host to install any third-party dependencies for widget testing in Flutter.

Implementation:

Steps to Implement Widget Testing

Step 1: Add the dependencies

Add the flutter_test dependency to pubspec — yaml file.

dev_dependencies:
  flutter_test:
    sdk: flutter

Step 2: Create a file "product_screen_test.dart" in the directory test/product/view/products/ with the following code.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:techamp_flutter_shopping_app/app.dart';

void main() {
  group('ProductsScreen', () {
    ProductsCubit productsCubit;

    setUp(() {
      productsCubit = _MockProductsCubit();
      getIt.registerFactory(() => productsCubit);
    });

    tearDown(() {
      productsCubit?.close();
      getIt.reset();
    });

    testWidgets('should show progress indicator on initial view',
        (tester) async {
      when(productsCubit.state).thenReturn(const InitialProductsState());
      await tester.pumpWidget(
        const MaterialApp(
          home: ProductsScreen(),
        ),
      );

      expect(find.byType(CircularProgressIndicator), findsOneWidget);
    });

    testWidgets('should show products when loaded successfully',
        (tester) async {
      const products = [
        Product(
          id: 1,
          title: 'Fjallraven',
          image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg',
          price: 109.95,
          description: 'Your perfect pack for everyday use.',
          category: MenClothingProductCategory(),
        ),
        Product(
          id: 2,
          title: 'Mens Casual Premium T-Shirts',
          image:
              'https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg',
          price: 22.5,
          description: 'Slim-fitting style.',
          category: MenClothingProductCategory(),
        )
      ];
      when(productsCubit.state)
          .thenReturn(const ProductsLoadedState(products: products));

      // Fixes Image.network network exception
      HttpOverrides.global = null;

      await tester.pumpWidget(
        const MaterialApp(
          home: ProductsScreen(),
        ),
      );

      expect(find.text(products.first.title), findsOneWidget);
      expect(find.text('${products.first.price} ETB'), findsOneWidget);

      expect(find.text(products.last.title), findsOneWidget);
      expect(find.text('${products.last.price} ETB'), findsOneWidget);
    });
  });
}

class _MockProductsCubit extends Mock implements ProductsCubit {}

Last updated