Coder Social home page Coder Social logo

Comments (4)

atn832 avatar atn832 commented on August 26, 2024

Basically, FakeFirebaseFirestore() acts like FirebaseFirestore.instance. It implements the same API. So in your code, instead of using directly FirebaseFirestore.instance, you should actually use an instance of FirebaseFirestore, be it the real one (FirebaseFirestore.instance) or the fake one (FakeFirebaseFirestore()).

The error message you are getting, which states that you did not call Firebase.initializeApp, shows that you are still running the real Firebase code (probably FirebaseFirestore.instance) somewhere in your test.

You can refer to https://firebase.flutter.dev/docs/testing/testing/, in which I give the full code to testing a simple app. You can see that only main() calls Firebase.initializeApp, and passes a real FirebaseFirestore instance to the Widget. The Widget is then initialized with an instance of FirebaseFirestore. On the opposite, during the tests, we pass aFakeFirebaseFirestore instead of the real implementation to the Widget.

Let me know if you still have issues!

from fake_cloud_firestore.

AdrienLemaire avatar AdrienLemaire commented on August 26, 2024

@atn832 thank you for taking the time to reply during the weekend.
I see. If the firestore instance is a widget parameter, we can easily replace the real instance with a fake one when calling the widget.

Are best practices discussed somewhere ?
I'm new to flutter, and I suppose you don't define a firestore parameter on every screen widget, passing firestore around with each go_router route.

If I lift the firestore state up to the top App widget with provider, then use Consumer to retrieve the firestore data in my components, I would need to wrap all related test components in a FakeApp widget.
Would that be a clean solution ?

Something like:

Widget createHomeScreen() => ChangeNotifierProvider(
      create: (context) => FakeFirebaseFirestore(),
      child: MaterialApp(home: HomeScreen());
    );

void main() {
  group('Home Screen Tests', () {
    testWidgets('Testing content', (tester) async {
      await tester.pumpWidget(createHomeScreen());
    });
  });
}

Looking around, I found 2 interesting articles:

from fake_cloud_firestore.

atn832 avatar atn832 commented on August 26, 2024

Yes the sample code you propose is similar to what I use in my projects. I use InheritedWidget simply because it works well enough for my needs and doesn't add another dependency to the project. But the Provider API looks nice too!

Also I did not implement a FirebaseWaiter component because my app waits for several Future's. So it was easier to inline the FutureBuilder like this:

WidgetsFlutterBinding.ensureInitialized();
runApp(
  FutureBuilder(
    future: Future.wait([
      SharedPreferences.getInstance(),
      Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform)
    ]),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return [waiting state widget];
      }
      final sharedPreferences = snapshot.data.first as SharedPreferences;
      return MyApp(sharedPreferences);
    },
  ),
);

Finally, the first article's approach works, but it is quite complicated: they introduce an interface abstraction, implement the fake CounterManager and in the end manage to test only the Widget. It does not test the FirestoreCounterManager nor the fake CounterManager. If they wanted to be thorough, it would require even more work.

I haven't read the second article, but thanks for sharing! I'll read it later.

I haven't checked the official Flutter docs in a while, so I don't know if those practices are documented anywhere.

from fake_cloud_firestore.

AdrienLemaire avatar AdrienLemaire commented on August 26, 2024

Thanks!
I now understand that the implementation differs depending on the code architecture and design patterns chosen.
It would be nice to have some examples for popular state management systems to get new flutter developers up to speed faster, but it's probably a bit out of scope for this library :)

from fake_cloud_firestore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.