diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 345c691..c08bcdc 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -116,6 +116,36 @@ expect(people, hasLength(3)); expect(valid, isTrue); ``` +## Use string expression with types + +If you're referencing a type within a test description, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type: + +Bad ❗️ + +```dart +testWidgets('renders YourView', (tester) async {}); +``` + +Good ✅ + +```dart +testWidgets('renders $YourView', (tester) async {}); +``` + +If your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, consider omitting the string expression: + +Bad ❗️ + +```dart +group('$YourView', () {}); +``` + +Good ✅ + +```dart +group(YourView, () {}); +``` + ## Descriptive test Don't be afraid of being verbose in your tests. Make sure everything is readable, which can make it easier to maintain over time. @@ -131,12 +161,10 @@ blocTest('emits',); Good ✅ ```dart -testWidgets('renders YourView', (tester) async {}); -testWidgets('renders YourView for YourState', (tester) async {}); +testWidgets('renders $YourView', (tester) async {}); +testWidgets('renders $YourView for $YourState', (tester) async {}); test('given an [input] is returning the [output] expected', () async {}); -blocTest('emits StateA if ...',); - - +blocTest('emits $StateA if ...',); ``` ## Test with a single purpose @@ -146,14 +174,14 @@ Aim to test one scenario per test. You might end up with more tests in the codeb Bad ❗️ ```dart -testWidgets('renders widgetA and widgetB', (tester) async {}); +testWidgets('renders $WidgetA and $WidgetB', (tester) async {}); ``` Good ✅ ```dart -testWidgets('renders widgetA', (tester) async {}); -testWidgets('renders widgetB', (tester) async {}); +testWidgets('renders $WidgetA', (tester) async {}); +testWidgets('renders $WidgetB', (tester) async {}); ``` ## Use keys carefully @@ -227,7 +255,7 @@ void main() { // mock api client methods... }); - group('UserRepository', () { + group(UserRepository, () { // Tests... }); } @@ -237,7 +265,7 @@ Good ✅ ```dart void main() { - group('UserRepository', () { + group(UserRepository, () { late ApiClient apiClient; setUp(() { @@ -273,7 +301,7 @@ class _MySubject { } void main() { - group('$_MySubject', () { + group(_MySubject, () { final _MySubjectDependency myDependency = _MySubjectDependency(); test('value starts at 0', () { @@ -299,7 +327,7 @@ Good ✅ ```dart void main() { - group('$_MySubject', () { + group(_MySubject, () { late _MySubjectDependency myDependency; setUp(() {