1

I've got this test:

test(
    'Form is Edit, and value is different to form value, sets form type to Add, and sets hasFormTypeChanged to true',
    () async {
  manageVgnItmStore = MockManageVgnItmStore();
  final formVm = MockManageVgnItmsFormStore();
  when(formVm.hasFormTypeChanged).thenReturn(false);
  when(manageVgnItmStore?.lastSelectedVgnItm).thenReturn(
      const VgnItm.groceryItm(companyName: 'Cadbury', name: 'Chocolate'));
  when(manageVgnItmStore?.stateNotifiersVm)
      .thenReturn(ManageVgnItmsStateNotifiersStore(manageVgnItmStore!));
  when(manageVgnItmStore?.formVm).thenReturn(formVm);
  when(manageVgnItmStore?.formVm.hasFormTypeChanged).thenReturn(false);
  when(manageVgnItmStore?.formType).thenReturn(const Edit(name: 'Edit'));

  underTest = VgnItmFormEvents(manageVgnItmStore!);

  underTest?.onNameChanged('fasdfsdfsdf', form!);
  verify(underTest?.manageVgnItmStore.formType = const Add(name: 'Add'));
  verify(underTest?.manageVgnItmStore.formVm.hasFormTypeChanged = true);
});

The last verify call produces the error:

UnimplementedError: hasFormTypeChanged

package:test_api
Fake.noSuchMethod

package:vepo/src/presentation/stores/manage_vegan_items/form_store.dart 19:8 _FakeManageVgnItmsFormStore_0.hasFormTypeChanged

As you can see I have set it multiple times:

when(formVm.hasFormTypeChanged).thenReturn(false);
when(manageVgnItmStore?.formVm.hasFormTypeChanged).thenReturn(false);

Why is this error occurring?

BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
  • 1
    `when(...)` works on `Mock`s. Your error indicates you're attempting to invoke a method on a `Fake`. If you're using `Fake`s, then the expectation is that your `Fake` implementation will override the specific methods that it needs to simulate. – jamesdlin Jun 26 '22 at 04:15
  • Thanks. I didn't specify for it to be a fake, do you know why it is a fake? This is how I made the mock: `@GenerateMocks([ManageVgnItmsFormStore]) void main() {}`. I just want to verify that `hasFormTypeChanged` has been set to `true` during the invocation of `underTest?.onNameChanged('fasdfsdfsdf', form!);` so I think I want a mock, not a fake. – BeniaminoBaggins Jun 26 '22 at 04:47
  • I have no idea where the `Fake` is coming from. I do not think that `Fake`s are ever generated; typically you'd need an explicit class definition. Does the rest of the stacktrace have any clues? Are you sure you're consuming `MockManageVgnItmsFormStore` from the generated `.mocks.dart` file? Does the generated code look reasonable (i.e., the `MockManageVgnItmsFormStore` class extends `Mock` and not `Fake`)? – jamesdlin Jun 26 '22 at 05:00

0 Answers0