Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing-friendly dependency injection. #237

Open
bhoomikshetty opened this issue Jan 4, 2024 · 0 comments
Open

Testing-friendly dependency injection. #237

bhoomikshetty opened this issue Jan 4, 2024 · 0 comments

Comments

@bhoomikshetty
Copy link
Contributor

🚀 The feature

Problem

Usage of Get.find<>() making it difficult to test the application as
proper dependency injection of mock controllers is not possible.

example
e.g.

class DiscussionsController extends GetxController {
  final Databases databases = AppwriteService.getDatabases();
  AuthStateController authStateController = Get.find<AuthStateController>();
  final CreateRoomController createRoomController = Get.find<CreateRoomController>();
  final TabViewController controller = Get.find<TabViewController>();
  final ThemeController themeController = Get.find<ThemeController>();
  final RoomsController roomsController = Get.find<RoomsController>();
..........
}

Solution

Constructor injection, as the mock dependencies can be injected easily.

e.g.

class DiscussionsController extends GetxController {
  final Databases databases;
  final AuthStateController authStateController;
  final CreateRoomController createRoomController;
  final TabViewController controller;
  final  ThemeController themeController;
  final RoomsController roomsController;

  DiscussionsController({
    required this.authStateController,
    required this.createRoomController,
    required this.controller,
    required this.themeController,
    required this.roomsController,
  });
..........
}
Get.lazyPut(
      () => DiscussionsController(
        authStateController: Get.find<AuthStateController>(),
        controller: Get.find<TabViewController>(),
        createRoomController: Get.find<CreateRoomController>(),
        roomsController: Get.find<RoomsController>(),
        themeController: Get.find<ThemeController>(),
      ),
    );

In this case injecting Mock Controller can be injected and tested easily

Get.lazyPut(
      () => DiscussionsController(
        authStateController: Get.find<MockAuthStateController>(),
        controller: Get.find<MockTabViewController>(),
        createRoomController: Get.find<MockCreateRoomController>(),
        roomsController: Get.find<MockRoomsController>(),
        themeController: Get.find<MockThemeController>(),
      ),
    );

Motivation, pitch

When testing an app, using mock controllers is crucial. It's particularly convenient if dependencies are injected through constructors because it makes it easy to switch between actual and mock controllers. This flexibility enhances adaptability in testing, ensuring smooth transitions for different testing scenarios and improving overall testability and maintainability.

Can I work on this issue @chandansgowda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant