flutter_vibration icon indicating copy to clipboard operation
flutter_vibration copied to clipboard

Avoid static methods so we can mock it

Open paulononaka opened this issue 2 years ago • 1 comments

Hi. I was wondering if you could make it to use class methods instead of static ones, so we could mock it on Unit tests (perhaps using Singleton pattern would help?). Thanks.

paulononaka avatar Oct 06 '23 21:10 paulononaka

You can mock

You could do this

class VibrationController {
  static Future<bool?> Function() hasCustomVibrationsSupport = Vibration.hasCustomVibrationsSupport;
  static Future<void> Function({int duration}) vibrate = Vibration.vibrate;


  Future<void> call(int duration) async {
      await VibrationService.vibrate(duration: duration);
  }
}

So in the test

  test('Test Vibration', () {
    var called = 0;

    VibrationController .vibrate = ({int duration = 500}) async {
      called++;
    };
    VibrationController .hasCustomVibrationsSupport = () async {
      return true;
    };
    sut.call(
      parameters: {
        'duration': 200,
      },
    );
      expect(called, 1);
      expect(durationCount, 200);
  });

toshiossada avatar Nov 28 '23 14:11 toshiossada