Command Palette

Search for a command to run...

Назад к скиллам
скилл № 45946

unit-test

Write and maintain unit tests for Flutter BLoCs, Cubits, TaskEither Repositories, and Models using bloc_test and mocktail. Trigger when asked to test, add…

БесплатноБез исполняемых скриптов
7spanот 7span★ 43 звезды на GitHub

Установить unit-test в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Об этом скилле

Unit Test Skill

Guide for authoring unit tests in this Flutter Monorepo across BLoCs, Cubits, Repositories (TaskEither), and Models.


1. Test File Placement & Naming

Mirror the source directory structure inside the test/ folder of the owning package/app:

apps/app_core/test/modules/[feature]/
├── bloc/
│   └── [feature]_bloc_test.dart
├── repository/
│   └── [feature]_repository_test.dart
└── model/
    └── [feature]_model_test.dart

2. BLoC & Cubit Unit Tests (bloc_test)

Always use package:bloc_test and package:mocktail.

Pattern:

import 'package:app_core/modules/[feature]/bloc/[feature]_bloc.dart';
import 'package:app_core/modules/[feature]/model/[feature]_model.dart';
import 'package:app_core/modules/[feature]/repository/[feature]_repository.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fpdart/fpdart.dart';
import 'package:mocktail/mocktail.dart';

class Mock[Feature]Repository extends Mock implements I[Feature]Repository {}

void main() {
  late Mock[Feature]Repository mockRepository;
  late [Feature]Bloc bloc;

  setUp(() {
    mockRepository = Mock[Feature]Repository();
    bloc = [Feature]Bloc(repository: mockRepository);
  });

  tearDown(() {
    bloc.close();
  });

  group('[Feature]Bloc', () {
    test('initial state is correct', () {
      expect(bloc.state, equals(const [Feature]State()));
    });

    blocTest<[Feature]Bloc, [Feature]State>(
      'emits [loading, loaded] when [Feature]FetchDataEvent succeeds',
      setUp: () {
        when(() => mockRepository.fetchData()).thenReturn(
          TaskEither.right(const [/* mock model data */]),
        );
      },
      build: () => [Feature]Bloc(repository: mockRepository),
      act: (bloc) => bloc.add(const [Feature]FetchDataEvent()),
      expect: () => [
        const [Feature]State(status: ApiStatus.loading),
        isA<[Feature]State>()
            .having((s) => s.status, 'status', ApiStatus.loaded)
            .having((s) => s.data, 'data', isNotEmpty),
      ],
      verify: (_) {
        verify(() => mockRepository.fetchData()).called(1);
      },
    );

    blocTest<[Feature]Bloc, [Feature]State>(
      'emits [loading, error] when [Feature]FetchDataEvent fails',
      setUp: () {
        when(() => mockRepository.fetchData()).thenReturn(
          TaskEither.left(APIFailure(error: 'Network error')),
        );
      },
      build: () => [Feature]Bloc(repository: mockRepository),
      act: (bloc) => bloc.add(const [Feature]FetchDataEvent()),
      expect: () => [
        const [Feature]State(status: ApiStatus.loading),
        isA<[Feature]State>()
            .having((s) => s.status, 'status', ApiStatus.error)
            .having((s) => s.errorMessage, 'errorMessage', isNotEmpty),
      ],
    );
  });
}

3. Repository Tests with TaskEither

Repositories in this monorepo return TaskEither<Failure, T>. Test both Left and Right branches using .run():

void main() {
  group('[Feature]Repository', () {
    test('returns Right(data) on 200 HTTP response', () async {
      final resultEither = await repository.fetchData().run();

      expect(resultEither.isRight(), isTrue);
      resultEither.fold(
        (failure) => fail('Expected Right, got Left: $failure'),
        (data) => expect(data, isNotEmpty),
      );
    });

    test('returns Left(APIFailure) on error response', () async {
      final resultEither = await repository.fetchData().run();

      expect(resultEither.isLeft(), isTrue);
      resultEither.fold(
        (failure) => expect(failure, isA<APIFailure>()),
        (data) => fail('Expected Left, got Right: $data'),
      );
    });
  });
}

4. Model Tests (fromJson & toJson)

Ensure models serialize and deserialize without loss:

void main() {
  group('[Feature]Model', () {
    const jsonMap = {
      'id': 1,
      'title': 'Test title',
    };

    test('fromJson constructs valid model instance', () {
      final model = [Feature]Model.fromJson(jsonMap);
      expect(model.id, equals(1));
      expect(model.title, equals('Test title'));
    });

    test('toJson returns matching map', () {
      final model = [Feature]Model.fromJson(jsonMap);
      expect(model.toJson(), equals(jsonMap));
    });
  });
}

5. Verification Protocol

Run targeted test commands:

flutter test apps/app_core/test/modules/[feature]/bloc/[feature]_bloc_test.dart
flutter analyze

All tests must pass with zero analyzer warnings.

Полный SKILL.md на GitHub

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл unit-test?

Write and maintain unit tests for Flutter BLoCs, Cubits, TaskEither Repositories, and Models using bloc_test and mocktail. Trigger when asked to test, add unit tests, or verify logic in this monorepo.

Как установить скилл unit-test?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл unit-test запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить unit-test с