title: How to unit test whether the ChangeNotifier's notifyListeners was called in Flutter/Dart?
body: <p>I'm using the <code>provider</code> package in our app and I want to test my <code>ChangeNotifier</code> class individually to have simple unit tests checking the business logic.</p>
  
  <p>Apart from the values of <code>ChangeNotifier</code> properties, I also want to ensure that in certain cases (where necessary), the <code>notifyListeners</code> has been called, as otherwise, the widgets that rely on up-to-date information from this class would not be updated.</p>
  
  <p><em>Currently, I'm indirectly testing whether the <code>notifyListeners</code> have been called: I'm using the fact that the <code>ChangeNotifier</code> lets me add a callback using its <code>addListener</code> method. In the callback that I add in our testing suite, I simply increment an integer counter variable and make assertions on that.</em></p>
  
  <p><strong>Is this the right way to test whether my <code>ChangeNotifier</code> calls its listeners?</strong> Is there a more descriptive way of testing this?</p>
  
  <p>Here is the class I'm testing (simplified, so I can share it on StackOverflow):</p>
  
  <pre><code>import 'package:flutter/foundation.dart';
  
  class ExampleModel extends ChangeNotifier {
    int _value = 0;
  
    int get value =&gt; _value;
  
    void increment() {
      _value++;
      notifyListeners();
    }
  }
  </code></pre>
  
  <p>and this is how I test it:</p>
  
  <pre><code>import 'package:mobile_app/example_model.dart';
  import 'package:test/test.dart';
  
  void main() {
    group('$ExampleModel', () {
      ExampleModel exampleModel;
      int listenerCallCount;
  
      setUp(() {
        listenerCallCount = 0;
        exampleModel = ExampleModel()
          ..addListener(() {
            listenerCallCount += 1;
          });
      });
  
      test('increments value and calls listeners', () {
        exampleModel.increment();
        expect(exampleModel.value, 1);
        exampleModel.increment();
        expect(listenerCallCount, 2);
      });
  
      test('unit tests are independent from each other', () {
        exampleModel.increment();
        expect(exampleModel.value, 1);
        exampleModel.increment();
        expect(listenerCallCount, 2);
      });
    });
  }
  </code></pre>
  
  <p>Also, if you think testing this differently would be better, please let me know, I'm currently working as a solo Flutter dev on the team, so it's difficult to discover if I'm on the wrong track.</p>