dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
636 stars 163 forks source link

How can we stub a dependency which is injected directly into a function without using parameters ? #317

Closed thechinkysight closed 3 years ago

thechinkysight commented 3 years ago

I have a class like this :

class ViewModel {
  String get getFirstName=>"Tenzin";

  String getFullName() {
    // I want to stub this getter
    String firstName = getFirstName;

    String lastName = 'Lhamo';

    return firstName + ' ' + lastName;
  }
}

Inside the test:

void main(){
 test('description', () {
    ViewModel viewModel = ViewModel();
    // How can I stub getFirstName getter to return 'Tsering' instead of 'Tenzin' 
   expect(viewModel.getFullName(), 'Tsering Lhamo');

  });
}
srawlins commented 3 years ago

You can stub getFirstName:

when(viewModel.getFirstName).thenReturn('Tsering');

But viewModel should be a Mock, not just a ViewModel.

thechinkysight commented 3 years ago

You can stub getFirstName:

when(viewModel.getFirstName).thenReturn('Tsering');

But viewModel should be a Mock, not just a ViewModel.

Not working sir

class ViewModel {
  String get getFirstName => "Tenzin";

  String getFullName() {
    // I want to stub this getter
    String firstName = getFirstName;

    String lastName = 'Lhamo';

    return firstName + ' ' + lastName;
  }
}

class MockViewModel extends Mock implements ViewModel {}

void main() {
  test('description', () {
    MockViewModel viewModel = MockViewModel();
    when(viewModel.getFirstName).thenReturn('Tsering');
    expect(viewModel.getFullName(), 'Tsering Lhamo');
  });
}

Test Output is like :

Expected: 'Tsering Lhamo'
  Actual: <null>
   Which: not an <Instance of 'String'>

The Docs States : By default, any instance method of the mock instance returns null. The when, thenReturn, thenAnswer, and thenThrow APIs provide a stubbing mechanism to override this behavior.

srawlins commented 3 years ago

When you extended Mock, and implemented ViewModel, you no longer have ViewModel's implementation of getFullName. Since you have not stubbed getFullName, it returns null.

thechinkysight commented 3 years ago

When you extended Mock, and implemented ViewModel, you no longer have ViewModel's implementation of getFullName. Since you have not stubbed getFullName, it returns null.

Yeah so is there any way around for my problem ?

srawlins commented 3 years ago

I think I understand your question. You want some methods to be stubbed, and some to be real. No, I don't think mockito can solve this problem for you. You can just make a simple override:

class TestViewModel extends ViewModel {
  String getFullName = 'Tsering';
}
thechinkysight commented 3 years ago

I think I understand your question. You want some methods to be stubbed, and some to be real. No, I don't think mockito can solve this problem for you. You can just make a simple override:

class TestViewModel extends ViewModel {
  String getFullName = 'Tsering';
}

ahh poor me :( never mind I hope it will be in future