getx icon indicating copy to clipboard operation
getx copied to clipboard

Get.arguments coming Null for Unit test

Open utsavDave97 opened this issue 1 year ago • 0 comments

Describe the bug I want to unit test my controller but the Get.arguments is coming in as null. How can I populate Get.arguments?

Reproduction code NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)

example:

void main() => runApp(MaterialApp(home: Home()));

class Home extends StatelessWidget {
  @override
  Widget build(context) => Scaffold(
      appBar: AppBar(title: Text("counter")),
      body: Center(
        child: Text("Home"),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () => Get.toNamed('/name',arguments: 'John'),
      ));
}

class NameScreen extends StatelessWidget {
  final NameController controller = Get.put(NameController());
  @override
  Widget build(context) => Scaffold(
      appBar: AppBar(title: Text("App bar")),
      body: Center(
        child: Obx(() => Text(controller.name)),
      ));
}

class NameController extends GetxController {
  final String name = Get.arguments;
}

Unit test code:

void main() {
  group('NameScreen Widget Test', () {

    testWidgets('Renders NameScreen', (WidgetTester tester) async {
      // Build our app and trigger a frame.
      await tester.pumpWidget(GetMaterialApp(home: NameScreen()));

      // Verify the title text
      expect(find.text('John'), findsOneWidget); // ERROR HERE
  });
}

Expected behavior How can I pass arguments to my NameController unit test

Screenshots If applicable, add screenshots to help explain your problem.

Flutter Version: 3.16.5

Getx Version: 4.6.6

Describe on which device you found the bug: N/A

Minimal reproduce code Provide a minimum reproduction code for the problem

utsavDave97 avatar Jan 16 '24 21:01 utsavDave97