testController<S extends SuperController, T> function

  1. @isTest
void testController<S extends SuperController, T>(
  1. String description, {
  2. required S build(),
  3. required T state(
    1. S controller
    ),
  4. void onEnable(
    1. S controller
    )?,
  5. void onAlive(
    1. S controller
    )?,
  6. void onDisable(
    1. S controller
    )?,
  7. FutureOr<void> setUp()?,
  8. T seed()?,
  9. FutureOr<void> act(
    1. S controller
    )?,
  10. Duration? wait,
  11. int skip = 0,
  12. List<T> expect()?,
  13. FutureOr<void> verify(
    1. S controller
    )?,
  14. Object errors()?,
  15. FutureOr<void> tearDown()?,
  16. dynamic tags,
})

Test case for a SuperController object.

The testController function is used to define a test case for a SuperController object. It takes various parameters to configure the test case, including the description of the test, the build function to create the SuperController object, functions for handling enable, alive, and disable states, setup and teardown functions, initial state seed, actions to perform on the controller, a wait duration, expected outcomes, verification functions, error handling, and optional tags for categorization.

Example usage:

testController<MyController, int>(
  'MyController test',
  build: () => MyController(),
  state: (controller) => controller.myState,
  onEnable: () {
    // Handle the enable state.
  },
  onAlive: () {
    // Handle the alive state.
  },
  onDisable: () {
    // Handle the disable state.
  },
  setUp: () {
    // Perform setup operations.
  },
  seed: () => 10,
  act: (controller) async {
    // Perform actions on the controller.
  },
  wait: const Duration(seconds: 1),
  expect: () => [10],
  verify: (controller) async {
    // Perform additional verifications.
  },
  tearDown: () {
    // Perform teardown operations.
  },
);

In the above example, the testController function is used to define a test case for a custom SuperController object that holds an integer value. The test case includes a description, a build function to create the SuperController object, functions to handle the enable, alive, and disable states, setup and teardown functions, an initial state seed, actions to perform on the controller, a wait duration, expected outcomes, verification functions, and teardown operations. The test case can be executed by running the test suite.

The testController function provides a convenient way to define and organize test cases for SuperController objects in unit tests.

Implementation

@isTest
void testController<S extends SuperController, T>(
  String description, {
  required S Function() build,
  required T Function(S controller) state,
  void Function(S controller)? onEnable,
  void Function(S controller)? onAlive,
  void Function(S controller)? onDisable,
  FutureOr<void> Function()? setUp,
  T Function()? seed,
  FutureOr<void> Function(S controller)? act,
  Duration? wait,
  int skip = 0,
  List<T> Function()? expect,
  FutureOr<void> Function(S controller)? verify,
  Object Function()? errors,
  FutureOr<void> Function()? tearDown,
  dynamic tags,
}) {
  test.test(
    description,
    () async {
      await _controllerTest<S, T>(
        setUp: setUp,
        build: build,
        state: state,
        seed: seed,
        onEnable: onEnable,
        onAlive: onAlive,
        onDisable: onDisable,
        act: act,
        wait: wait,
        skip: skip,
        expect: expect,
        verify: verify,
        errors: errors,
        tearDown: tearDown,
      );
    },
    tags: tags,
  );
}