How to test if a method was called in Dart/Flutter + MobX?

Viewed 430

I use MobX for state management in my Flutter app and my store has an init() method, that is used to populate the state with all the information that is needed on startup:

MyStore {
  ...
  bool _initialized = false;
  ...
  void init() {
    if (!_initialized) {
      fetchInfo();
      _initialized = true;
    }
  }
  ...
  @action
  void fetchInfo() { alter observables of store }
}

As I didn't commit to TDD, I'm now in the position to write unit tests after the implementation and I have no idea how to test that e.g. init calls fetchInfo and only really runs once. If fetchInfo was in another class, I could mock it, but as it alters observables of MyStore, I don't think it could be extracted.

Does anyone have an idea on how to test this or how to refactor this? Thanks

1 Answers

If one is using fetchInfo to retrive the data from outside, it's there one possibility to test, since we can supose that this method will call some repository. But if the method it's only used to set data, one can verify the data directly in the test after the calls.

The method fetchInfo seems to be a private method, so, set the mark _ to it, if not the case ignore this, but if it's the case, you should not worrie about private methods in your Unit Tests, because they are internal and they can't be tested this way, because they only concearn to the internal functions

Related