Skip to content

Instantly share code, notes, and snippets.

@vindolin
Last active March 10, 2023 12:05
Show Gist options
  • Save vindolin/ccb04a3382871da90743e845d4bfdb49 to your computer and use it in GitHub Desktop.
Save vindolin/ccb04a3382871da90743e845d4bfdb49 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'fake_test.g.dart';
@riverpod
class TheNumber extends _$TheNumber {
@override
int build() {
return 42;
}
}
class TheNumberWidget extends ConsumerWidget {
const TheNumberWidget({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theNumber = ref.watch(theNumberProvider);
return Text('TheNumber: $theNumber');
}
}
@riverpod
class FakeNumber extends _$FakeNumber {
@override
int build() {
return 43;
}
}
void main() {
group(
'TheNumber Tests',
() {
testWidgets(
'Testing TheNumber',
(tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
theNumberProvider.overrideWith(
(ref) {
return FakeNumber();
},
),
],
child: MaterialApp(
home: Scaffold(
body: Consumer(
builder: (context, ref, _) {
return Stack(
children: const [
Positioned(
top: 0,
left: 0,
child: TheNumberWidget(),
),
],
);
},
),
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('TheNumber: 43'), findsOneWidget, reason: 'TheNumber should be 43');
},
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment