Skip to content

Commit

Permalink
. d updated README.md file: added snippets for example with model
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 11, 2024
1 parent 252dacc commit 2d67c73
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 70 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.4.1

- Autopublish with new tags.
- Autopublish to pub.dev if the version in pubspec.yaml has been changed by auto adding a new tag to the repositories.
- Codecov added to github actions. Codecov badge, graph was added to README.md.
- Added mdsnippets to github actions. And example snippets added to README.md.
- Added `ApprovalTests.Dart.StarterProject'. You can find it in the README.md file.

## 0.3.9

Expand Down
79 changes: 19 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ dependencies:
approval_tests: ^0.4.1
```
## 👀 Getting Started
The best way to get started is to download and open the starter project:
* [Approvaltests.Dart.StarterProject](https://github.com/approvals/Approvaltests.Dart.StarterProject)
This is a standard project that can be imported into any editor or IDE and also includes CI with GitHub Actions.
It comes ready with:
- A suitable `.gitignore` to exclude approval artifacts
- A ready linter with all rules in place
- A GitHub action to build and run tests
- A GitHub build status ToBadge


## 📚 How to use

### Approving Results
Expand Down Expand Up @@ -153,68 +168,12 @@ And the `verify_methods` folder has small examples of using different `ApprovalT

### JSON example

```dart
import 'package:approval_tests/approval_dart.dart';
import 'package:test/test.dart';
snippet: same_verify_as_json_test_with_model

void main() {
test('Verify JSON output of an object', () {
var item = Item(
id: 1,
name: "Widget",
anotherItem: AnotherItem(id: 1, name: "AnotherWidget"),
subItem: SubItem(
id: 1,
name: "SubWidget",
anotherItems: [
AnotherItem(id: 1, name: "AnotherWidget1"),
AnotherItem(id: 2, name: "AnotherWidget2"),
],
),
);
ApprovalTests.verifyAsJson(
item,
);
});
}
/// Item class for testing
class Item {
final int id;
final String name;
final SubItem subItem;
final AnotherItem anotherItem;
Item({
required this.id,
required this.name,
required this.subItem,
required this.anotherItem,
});
}
/// Sub item class for testing
class SubItem {
final int id;
final String name;
final List<AnotherItem> anotherItems;
SubItem({
required this.id,
required this.name,
required this.anotherItems,
});
}
/// Another item class for testing
class AnotherItem {
final int id;
final String name;
this will result in the following file
`verify_as_json_test.approved.txt`

AnotherItem({required this.id, required this.name});
}
```
snippet: verify_as_json_test.approved.txt

<img src="https://github.com/K1yoshiSho/packages_assets/blob/main/assets/approval_tests/passed.png?raw=true" alt="Passed test example" title="ApprovalTests" style="max-width: 800px;">

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"item": {
"jsonItem": {
"id": 1,
"name": "Widget",
"name": "JsonItem",
"subItem": {
"id": 1,
"name": "SubWidget",
"name": "SubItem",
"anotherItems": [
{
"id": 1,
"name": "AnotherWidget1"
"name": "AnotherItem 1"
},
{
"id": 2,
"name": "AnotherWidget2"
"name": "AnotherItem 2"
}
]
},
"anotherItem": {
"id": 1,
"name": "AnotherWidget"
"name": "AnotherItem"
}
}
}
25 changes: 22 additions & 3 deletions example/verify_methods/verify_as_json/verify_as_json_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

import '../../../test/approval_test.dart';
import '../../../test/models/item.dart';

// begin-snippet: same_verify_as_json_test_with_model
void main() {
const jsonItem = JsonItem(
id: 1,
name: "JsonItem",
anotherItem: AnotherItem(id: 1, name: "AnotherItem"),
subItem: SubItem(
id: 1,
name: "SubItem",
anotherItems: [
AnotherItem(id: 1, name: "AnotherItem 1"),
AnotherItem(id: 2, name: "AnotherItem 2"),
],
),
);

test('Verify JSON output of an object', () {
Approvals.verifyAsJson(
ApprovalTestHelper.jsonItem,
options: const Options(deleteReceivedFile: true),
jsonItem,
options: const Options(
deleteReceivedFile: true, // Automatically delete the received file after the test.
approveResult: true, // Approve the result automatically. You can remove this property after the approved file is created.
),
);
});
}
// end-snippet

0 comments on commit 2d67c73

Please sign in to comment.