mockable_gen 0.1.0 copy "mockable_gen: ^0.1.0" to clipboard
mockable_gen: ^0.1.0 copied to clipboard

Build_runner generator that emits .mock.g.dart files with realistic XxxMock.mock() / XxxMock.mockList(n) factories for classes annotated with @Mockable() from package:mockable.

mockable_gen #

pub package

build_runner generator for package:mockable. Emits a sibling xxx.mock.g.dart file containing XxxMock.mock() and XxxMock.mockList(count) factories for every class annotated with @Mockable().

Install #

dependencies:
  mockable: ^0.1.0

dev_dependencies:
  mockable_gen: ^0.1.0
  build_runner: ^2.4.13

Usage #

import 'package:mockable/mockable.dart';

part 'user.mock.g.dart';

@Mockable()
class User {
  final String id;
  final String email;
  final String fullName;

  const User({required this.id, required this.email, required this.fullName});
}

Run the builder:

dart run build_runner build

Generated output (user.mock.g.dart):

// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'user.dart';

extension UserMock on User {
  static User mock() => User(
        id: MockFaker.id(),
        email: MockFaker.email(),
        fullName: MockFaker.name(),
      );

  static List<User> mockList([int count = 10]) =>
      List.generate(count, (_) => UserMock.mock());
}

How it works #

For each class annotated with @Mockable():

  1. The generator picks a constructor (priority: unnamed generative or redirecting factory; otherwise the first named one that isn't fromJson/empty/mock and doesn't start with _).
  2. For each parameter, it picks a value via two layers:
    • Field-name heuristicsemailMockFaker.email(), phoneMockFaker.phone(), etc. (see the mockable README for the full table).
    • Type-based fallbackStringMockFaker.word(), intMockFaker.integer(), boolMockFaker.boolean(), DateTimeMockFaker.dateTime(), enums → first non-unknown/none value, nested @Mockable models → XxxMock.mock(), List<T>TMock.mockList(3) or List.generate(3, ...).
  3. Generates the XxxMock extension with mock() and mockList([int count = N]).

Pairs naturally with #

  • json_serializable — your existing @JsonSerializable() DTOs work as-is; just add @Mockable().
  • freezed — Freezed redirecting factories and @Default(...) are recognized.
  • skeletonizermockData: UserMock.mockList(8) produces realistic-width skeletons.

Edge cases #

  • Cyclic references (ABA) — the generator detects cycles and falls back to .empty() if available, else null for nullable fields, else the unnamed constructor with no args.
  • Generic classes (Class<T>) — skipped in v1 with a log message.
  • Manual override — if a hand-written extension XxxMock on Xxx already exists in the same library, the generator skips that class.
  • Custom @JsonKey(fromJson:) — emits null (or a TODO marker) so you can fill in the right value manually.
  • @MockableIgnore() — apply on a field to opt out of mock generation for that one field.

License #

MIT

0
likes
0
points
28
downloads

Publisher

unverified uploader

Weekly Downloads

Build_runner generator that emits .mock.g.dart files with realistic XxxMock.mock() / XxxMock.mockList(n) factories for classes annotated with @Mockable() from package:mockable.

Repository (GitHub)
View/report issues

Topics

#code-generation #mock #testing #skeletonizer

License

unknown (license)

Dependencies

analyzer, build, dart_style, meta, mockable, source_gen

More

Packages that depend on mockable_gen