0

I am writing test for ViewModel. In that process I don't have access to BuildContext. When I use as Strings.current.SOME_KEY in that scenario I got an error.

Here is my code:

void main() {
  late MyViewModel viewModel;

  setUpAll(() {});

  setUp(() {
    setupLocators();
    viewModel = MyViewModel(repository: Locators.repository);
  });

  test("MyViewModel has some initial data", () async {
    expect(viewModel.selectedColorType.value, -1 );
    viewModel.setSelectedColorType= const Color(title: "title", value: 1);

    expect(viewModel.selectedColor.value, 1);
  });
}

In this scenario I am facing following error:

Testing started at 11:48 AM ...

dart:core                                                                           _AssertionError._throwNew
package:my_mobile_app/generated/l10n.dart 21:12                                      Strings.current
package: my_mobile_app/view_models/colors/color_view_model.dart 11:13  new ColorsViewModel
test/view_models/colors/_view_model_test.dart 18:17             main.<fn>

'package: my_mobile_app/generated/l10n.dart': Failed assertion: line 21 pos 12: '_current != null': No instance of Strings was loaded. Try to initialize the Strings delegate before accessing Strings.current.

Is there any other way I can access these localized strings.

On ColorsViewModel screen I do have following declaration which shows in log:

final List<String> stepTitles = [
    Strings.current.firstPage,
    Strings.current.secondPage,
    Strings.current.thirdPage,
    Strings.current.fourthPage
  ];
Code Hunter
  • 10,075
  • 23
  • 72
  • 102

0 Answers0