Skip to content

Commit

Permalink
Merge pull request #18 from VGVentures/alestiago/docs-use-string-expr…
Browse files Browse the repository at this point in the history
…essions-with-types

docs: add "Use string expression with types" to testing practices
  • Loading branch information
jolexxa authored Jul 19, 2024
2 parents ca4c4e4 + da4b735 commit 2720113
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/content/docs/testing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -131,12 +161,10 @@ blocTest<YourBloc, RecipeGeneratorState>('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<YourBloc, RecipeGeneratorState>('emits StateA if ...',);
blocTest<YourBloc, RecipeGeneratorState>('emits $StateA if ...',);
```

## Test with a single purpose
Expand All @@ -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
Expand Down Expand Up @@ -227,7 +255,7 @@ void main() {
// mock api client methods...
});
group('UserRepository', () {
group(UserRepository, () {
// Tests...
});
}
Expand All @@ -237,7 +265,7 @@ Good ✅

```dart
void main() {
group('UserRepository', () {
group(UserRepository, () {
late ApiClient apiClient;
setUp(() {
Expand Down Expand Up @@ -273,7 +301,7 @@ class _MySubject {
}
void main() {
group('$_MySubject', () {
group(_MySubject, () {
final _MySubjectDependency myDependency = _MySubjectDependency();
test('value starts at 0', () {
Expand All @@ -299,7 +327,7 @@ Good ✅
```dart
void main() {
group('$_MySubject', () {
group(_MySubject, () {
late _MySubjectDependency myDependency;
setUp(() {
Expand Down

0 comments on commit 2720113

Please sign in to comment.