📒
Mobile Development Reference-TCE-C01
  • Welcome
  • Introduction
    • Project Description
  • First Steps
    • Creating Project
    • Setting up Workspace
      • Project structure
      • Setting up the app
  • Routing
    • Setting up auto route
    • Creating the first screen
    • Setting up router
    • Adding router to Material App
  • Products Feature
    • Creating models
      • Introduction to freezed
      • Introduction to JSON annotations
      • Creating products model
    • Creating products cubit
      • Creating Products States
      • Creating Products Cubit
      • Implementing Get Products Functionality
    • Creating products repository
      • Setting up Dio
      • Making your first http request with DIO
    • Implementing Products Screen UI
      • Adding Products Grid
      • Adding Products Tile
      • Add Products Error Widget
    • Consuming the products cubit
      • Setting up Dependency injection
      • Creating Bloc Provider for ProductsCubit
      • Mapping Products Cubit states to UI
  • Testing
    • Widget Testing
    • Unit Testing
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Testing

Unit Testing

Testing the product model

create product_test.dart file in test/product/model/ with the following code:

import 'package:flutter_test/flutter_test.dart';
import 'package:techamp_flutter_shopping_app/app.dart';

void main() {
  group('Product', () {
    test('fromJson should return Product with correct values', () {
      final json = {
        'id': 1,
        'price': 109.95,
        'title': 'Fjallraven',
        'description': 'description',
        'category': 'men clothing',
        'image': 'image',
      };

      final actual = Product.fromJson(json);

      expect(actual.id, 1);
      expect(actual.price, 109.95);
      expect(actual.image, 'image');
      expect(actual.description, 'description');
      expect(actual.category, const MenClothingProductCategory());
    });
  });
}
PreviousWidget Testing

Last updated 4 years ago

Was this helpful?