class ProfileViewModel extends ChangeNotifier {
ProfileViewModel(this._updateProfileBodyUseCase);
final UpdateProfileBodyUseCase _updateProfileBodyUseCase;
String _body = '';
String get body => _body;
void onBodyEdited(String body) {
_updateProfileBodyUseCase.execute(body: body).then((value) {
_body = value;
notifyListeners();
});
}
}
class UpdateProfileBodyUseCase {
Future<String> execute({required String body}) { ... }
}
test('test', () async {
when(updateProfileBodyUseCase.execute(body: anyNamed('body')))
.thenAnswer((_) => Future.value('body'));
final completer = Completer<void>();
viewModel.addListener(completer.complete);
viewModel.onBodyEdited('body');
await completer.future;
expect(viewModel.body, 'body');
});
@GenerateMocks([
UpdateProfileBodyUseCase,
])
void main() {
late MockUpdateProfileBodyUseCase updateProfileBodyUseCase;
late ProfileViewModel viewModel;
setUp(() {
updateProfileBodyUseCase = MockUpdateProfileBodyUseCase();
viewModel = ProfileViewModel(updateProfileBodyUseCase);
});
test('test', () async {
when(updateProfileBodyUseCase.execute(body: anyNamed('body')))
.thenAnswer((_) => Future.value('body'));
final completer = Completer<void>();
viewModel.addListener(completer.complete);
viewModel.onBodyEdited('body');
await completer.future;
expect(viewModel.body, 'body');
});
}