I have been trying to use flutter integration testing. now the code has api calls at several place, so I have modified in a way that a single dio client will be used for api calls, throughout the application as below.
class AppSession extends GetxController {
final Dio dio;
AppSession(this.dio);
//other methods and properties
}
// and this is my main.dart file contents,
AppSession get currentSession => Get.find<AppSession>();
void main() async {
Get.lazyPut(() => AppSession(Dio()));
runApp(const MyApp());
}
So everywhere in the file when i need a client, I'll call "currentSession.dio". Now I'm writing the integration testing for a login page as below. These are the contents of main.dart in integration_test.
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
final dio = Dio();
setUp(() {
Get.reset();
// Set up Dio with the adapter
Dio adjustedDio = setAdapter(dio);
// Provide the adjusted Dio instance to the AppSession
Get.lazyPut(() => AppSession(adjustedDio));
});
group('end-to-end test', () {
testWidgets('login with email and password test', (tester) async {
// Load app widget.
await tester.pumpWidget(const MyApp());
// other testing code...
Finder loginButton = find.text("LOGIN");
await tester.tap(loginButton);
// other testing code...
});
});
and I'm using "chromedriver" to test the application. "setAdapter(dio)", it takes the client and binds the "DioAdapter" from the "http_mock_adapter". Here i configured the hard-coded http requests and their corresponding responses
when the tester taps the login button, nothing happens and the tests are failed. The application is working fine on actual APIs but I could not write tests for that.