Home > Mobile >  Flutter Test throws "PlatformException(channel-error, Unable to establish connection on channel
Flutter Test throws "PlatformException(channel-error, Unable to establish connection on channel

Time:11-09

For some reason I keep getting this error "PlatformException(channel-error, Unable to establish connection on channel., null, null)" when running my flutter/dart test and I can't seem to solve it.

So I am working on an university software project, currently I am attempting to implement some tests.

The test is supposed to retrieve 5 questions from the database into the quiz instance then return them to me through getQuestions() and then just check if they are in the given list.

For some reason I keep getting this error "PlatformException(channel-error, Unable to establish connection on channel., null, null)" and I can't seem to solve it.

The firebase connection works normally in the app.

Anyone has any experience why this might be?

The data for the firebase connection is masked here by MyData to not share the project information.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:project/main.dart';
import 'package:project/model/question.dart';
import 'package:project/model/quiz.dart';
import 'package:test/test.dart';

void main() async{
  late Quiz quiz;
  WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp(
      options: const FirebaseOptions(
        apiKey: 'myAPIKey',
        appId: 'myApp',
        messagingSenderId: 'SenderID',
        projectId: 'ProjectID',
      ),
    );
  runApp(MyApp());
  test ('Check if quiz initialize works, and call the getter for the questions', () async {
    List<Question> _questions = [];
    quiz = Quiz(id: 1, noOfQuestions: 5, category: 'Science');
    _questions = quiz.getQuestions();
    expect(_questions.length, 5);
    expect(_questions[0].answers.length, 4);
  });
}

CodePudding user response:

When you are testing an app, you don't do that with a connection to your real database. Instead, you create a "Mock" of it. Mocks are simulated objects that mimic the behaviour of real objects in controlled ways.

Check out this video to learn more: https://youtu.be/hUAUAkIZmX0

Once you understood what mocks are, here is a package that helps you to mock your firebase database: https://pub.dev/packages/firebase_database_mocks

  • Related