Closed thechinkysight closed 3 years ago
You can stub getFirstName
:
when(viewModel.getFirstName).thenReturn('Tsering');
But viewModel
should be a Mock, not just a ViewModel.
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.
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
.
When you extended Mock, and implemented ViewModel, you no longer have ViewModel's implementation of
getFullName
. Since you have not stubbedgetFullName
, it returnsnull
.
Yeah so is there any way around for my problem ?
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';
}
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
I have a class like this :
Inside the test: