Rudy
Rudy

Reputation: 467

Flutter: Mock extension method using Mocktail (from EasyLocalization)

The flutter package easy_localization has an extention method on the BuildContext which looks like this:

  Locale get deviceLocale => EasyLocalization.of(this)!.deviceLocale;

I wish to mock this using Mocktail.

I tried:

import 'package:mocktail/mocktail.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:test/test.dart';

class MockBuildContext extends Mock implements BuildContext {}

void main() {
  test("Sample test", () {
    final context = MockBuildContext();
    when(() => context.deviceLocale).thenReturn(const Locale('en', 'US'));
    expect(Locale('en', 'US'), context.deviceLocale);
  });
}

However this throws an error: type 'Locale' is not a subtype of type '_EasyLocalizationProvider?'. I am assuming this happens because we are (unsuccessfully) trying to mock an Extension method and that the original method is called instead.

How can I mock the extension method to get the desired result?

Upvotes: 1

Views: 1707

Answers (0)

Related Questions