https://github.com/flutterando/value_listenable_test
Assists in testing ValueListenable objects (ex: ValueNotifier).
https://github.com/flutterando/value_listenable_test
dart
Last synced: 5 months ago
JSON representation
Assists in testing ValueListenable objects (ex: ValueNotifier).
- Host: GitHub
- URL: https://github.com/flutterando/value_listenable_test
- Owner: Flutterando
- License: other
- Created: 2022-01-04T15:14:23.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-30T03:12:40.000Z (over 2 years ago)
- Last Synced: 2025-04-19T18:54:40.688Z (10 months ago)
- Topics: dart
- Language: Dart
- Homepage: https://pub.dev/packages/value_listenable_test
- Size: 4.88 KB
- Stars: 5
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# value_listenable_test
Assists in testing **ValueListenable** objects (ex: **ValueNotifier**).
## install
Added in your `pubspec.yaml` as **dev dependency**:
```yaml
dev_dependencies:
value_listenable_test: any
```
## Unit test with emitValues and valueListenableTest
Listen the emits of ValueListenable:
```dart
test('valueListenable Matcher', () {
final counter = ValueNotifier(0);
expect(counter, emitValues([2, 3, 5]));
counter.value = 2;
counter.value = 3;
counter.value = 5;
});
```
Also, you can use the test abstraction called **valueListenableTest**:
```dart
valueListenableTest(
'Counter emits [1] when update method is called',
build: () => Counter(),
act: (notifier) => notifier.update(1),
expect: () => [1],
);
```
## Mock
It's easy, just extend the `MockValueListenable`:
```dart
class MockCounter extends MockValueListenable {}
```
To notify the listeners with a new value call the [callListeners] method.
```dart
final counter = MockCounter();
counter.addListener(() => print(counter.value)); // 50
when(() => counter.value).thenReturn(50);
print(counter.value); // 50
counter.callListeners();
```
## Stub the value
Creates a stub response for the `addListener` method on a `valueListenable`.
The `valueListenable` will have each `value` (in order) after the`input` is called.
Optionally provide an `initialValue` to stub the `value` of the `valueListenable`,
or also optionally provide a `delay` between the `values` change.
```dart
// Create a mock instance
final counter = CounterMock();
// Stub the value
whenListen(
counter,
input: counter.increment,
initialValue: 0,
values: [1, 2, 3],
);
// Assert that the initial value is correct.
expect(counter.value, 0);
// Verifies values after to call increment
valueListenableTest(
'value = [1, 2, 3] when increment is called',
build: () => counter,
act: (notifier) => notifier.increment(),
expect: () => [1, 2, 3],
);
```
>**Note:** When setting the **initialValue** the listeners are never called, then **emitValues**
>and **valueListenableTest** methods never must expect that.
That`s it!