Skip to content

Commit d7604ac

Browse files
Yaibaparlough
andauthored
tutorial/testing: Use intended function Article.listFromJson (#7185)
Fixes #7184, use intended function in the test example code. I don't think there is a corresponded code file related to this change, so only the markdown file is changed. --------- Co-authored-by: Parker Lougheed <parlough@gmail.com>
1 parent 5de8b7c commit d7604ac

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

src/content/learn/tutorial/testing.md

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,8 @@ You'll use the `group`, `test`, and `expect` functions from the `test` package.
285285
final String articleJson = await File(catExtractJson).readAsString();
286286
final Map<String, Object?> articleMap =
287287
jsonDecode(articleJson) as Map<String, Object?>;
288-
289-
final Map<String, Object?> pagesMap =
290-
(articleMap['query'] as Map)['pages'] as Map<String, Object?>;
291-
292-
// The 'pagesMap' contains a single key (e.g., '6678').
293-
// We get the first (and only) value from that map.
294-
final Map<String, Object?> catArticleMap =
295-
pagesMap.values.first as Map<String, Object?>;
296-
297-
final Article article = Article(
298-
title: catArticleMap['title'] as String,
299-
extract: catArticleMap['extract'] as String,
300-
);
301-
302-
expect(article.title.toLowerCase(), 'cat');
288+
final List<Article> articles = Article.listFromJson(articleMap);
289+
expect(articles.first.title.toLowerCase(), 'cat');
303290
});
304291
});
305292
}
@@ -308,9 +295,9 @@ You'll use the `group`, `test`, and `expect` functions from the `test` package.
308295
This `test` function does the following:
309296

310297
- Reads the contents of the `cat_extract.json` file.
311-
- Decodes the JSON string into a `List<Object?>`.
312-
- Creates the `Article` object from the list using
313-
the `Article.listFromJson` constructor.
298+
- Decodes the JSON string into a `Map<String, Object?>`.
299+
- Creates the `List<Article>` object from the map using
300+
the `Article.listFromJson` static method.
314301
- Uses the `expect` function to assert that
315302
the `title` property of the first article is equal to `'cat'`.
316303

@@ -332,25 +319,12 @@ You'll use the `group`, `test`, and `expect` functions from the `test` package.
332319
});
333320
334321
test('deserialize Cat article example data from json file into '
335-
'an Article object', () async {
322+
'an Article object', () async {
336323
final String articleJson = await File(catExtractJson).readAsString();
337324
final Map<String, Object?> articleMap =
338325
jsonDecode(articleJson) as Map<String, Object?>;
339-
340-
final Map<String, Object?> pagesMap =
341-
(articleMap['query'] as Map)['pages'] as Map<String, Object?>;
342-
343-
// The 'pagesMap' contains a single key (e.g., '6678').
344-
// We get the first (and only) value from that map.
345-
final Map<String, Object?> catArticleMap =
346-
pagesMap.values.first as Map<String, Object?>;
347-
348-
final Article article = Article(
349-
title: catArticleMap['title'] as String,
350-
extract: catArticleMap['extract'] as String,
351-
);
352-
353-
expect(article.title.toLowerCase(), 'cat');
326+
final List<Article> articles = Article.listFromJson(articleMap);
327+
expect(articles.first.title.toLowerCase(), 'cat');
354328
});
355329
356330
test('deserialize Open Search results example data from json file '

0 commit comments

Comments
 (0)