cubit_test 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new92

Cubit Test

Pub build coverage Star on GitHub License: MIT Starware

A Dart package built to make testing cubits easy. Built to work with the cubit and bloc state management packages.

Create a Mock Cubit #

import 'package:cubit_test/cubit_test.dart';

class MockCounterCubit extends MockCubit<int> implements CounterCubit {}

Stub the Cubit Stream #

whenListen creates a stub response for the listen method on a Cubit. Use whenListen if you want to return a canned Stream of states for a cubit instance. whenListen also handles stubbing the state of the cubit to stay in sync with the emitted state.

// Create a mock instance
final counterCubit = MockCounterCubit();

// Stub the cubit `Stream`
whenListen(counterCubit, Stream.fromIterable([0, 1, 2, 3]));

// Assert that the cubit emits the stubbed `Stream`.
await expectLater(counterCubit, emitsInOrder(<int>[0, 1, 2, 3])))

// Assert that the cubit's current state is in sync with the `Stream`.
expect(counterCubit.state, equals(3));

Unit Test a Real Cubit with cubitTest #

cubitTest creates a new cubit-specific test case with the given description. cubitTest will handle asserting that the cubit emits the expected states (in order) after act is executed. cubitTest also handles ensuring that no additional states are emitted by closing the cubit stream before evaluating the expectation.

build should be used for all cubit initialization and preparation and must return the cubit under test as a Future.

act is an optional callback which will be invoked with the cubit under test and should be used to interact with the cubit.

skip is an optional int which can be used to skip any number of states. The default value is 1 which skips the initialState of the cubit. skip can be overridden to include the initialState by setting skip to 0.

wait is an optional Duration which can be used to wait for async operations within the cubit under test such as debounceTime.

expect is an optional Iterable<State> which the cubit under test is expected to emit after act is executed.

verify is an optional callback which is invoked after expect and can be used for additional verification/assertions. verify is called with the cubit returned by build.

group('CounterCubit', () {
  cubitTest(
    'emits [] when nothing is called',
    build: () async => CounterCubit(),
    expect: [],
  );

  cubitTest(
    'emits [1] when increment is called',
    build: () async => CounterCubit(),
    act: (cubit) async => cubit.increment(),
    expect: [1],
  );
});

cubitTest can also be used to skip any number of emitted states before asserting against the expected states. The default value is 1 which skips the initialState of the cubit. skip can be overridden to include the initialState by setting skip to 0.

cubitTest(
  'CounterCubit emits [0, 1] when increment is called',
  build: () async => CounterCubit(),
  act: (cubit) async => cubit.increment(),
  skip: 0,
  expect: [0, 1],
);

cubitTest can also be used to wait for async operations like debounceTime by providing a Duration to wait.

cubitTest(
  'CounterCubit emits [1] when increment is called',
  build: () async => CounterCubit(),
  act: (cubit) async => cubit.increment(),
  wait: const Duration(milliseconds: 300),
  expect: [1],
);

cubitTest can also be used to verify internal cubit functionality.

cubitTest(
  'CounterCubit emits [1] when increment is called',
  build: () async => CounterCubit(),
  act: (cubit) async => cubit.increment(),
  expect: [1],
  verify: (_) async {
    verify(repository.someMethod(any)).called(1);
  }
);

Note: when using cubitTest with state classes which don't override == and hashCode you can provide an Iterable of matchers instead of explicit state instances.

cubitTest(
  'emits [StateB] when emitB is called',
  build: () async => MyCubit(),
  act: (cubit) async => cubit.emitB(),
  expect: [isA<StateB>()],
);

Dart Versions #

  • Dart 2: >= 2.7.0

Maintainers #

Starware #

Cubit Test is Starware.
This means you're free to use the project, as long as you star its GitHub repository.
Your appreciation makes us grow and glow up. ⭐

0.1.0 #

  • feat: upgrade to cubit: ^0.1.0

0.0.8 #

  • feat: upgrade to cubit: ^0.0.13
  • docs: various documentation updates

0.0.7 #

  • feat: update cubitTest to use CubitStream instead of Cubit
  • feat: update whenListen to use CubitStream instead of Cubit

0.0.6 #

  • feat: upgrade to cubit: ^0.0.10
  • docs: various documentation updates

0.0.5 #

  • BREAKING: upgrade to cubit: ^0.0.8
  • docs: minor logo updates

0.0.4 #

  • BREAKING: upgrade to cubit: ^0.0.6
  • fix: remove direct dependency on pedantic

0.0.3 #

  • BREAKING: upgrade to cubit: ^0.0.4

0.0.2 #

  • fix: Flutter v1.17.3 compatibility by adjusting to test ^0.14.0

0.0.1 #

  • feat: initial release

example/main.dart

import 'dart:async';

import 'package:cubit/cubit.dart';
import 'package:cubit_test/cubit_test.dart';
import 'package:test/test.dart';

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() => emit(state + 1);
}

// Mock Cubit
class MockCounterCubit extends MockCubit<int> implements CounterCubit {}

void main() {
  group('whenListen', () {
    test("Let's mock the CounterCubit's stream!", () {
      // Create mock CounterCubit instance.
      final counterCubit = MockCounterCubit();

      // Stub the listen with a fake Stream.
      whenListen<CounterCubit, int>(
        counterCubit,
        Stream.fromIterable([0, 1, 2, 3]),
      );

      // Expect that the CounterCubit instance emits the stubbed states.
      expectLater(counterCubit, emitsInOrder(<int>[0, 1, 2, 3]));
    });
  });

  group('cubitTest', () {
    cubitTest<CounterCubit, int>(
      'emits [] when nothing is called',
      build: () async => CounterCubit(),
      expect: <int>[],
    );

    cubitTest<CounterCubit, int>(
      'emits [1] when increment is called',
      build: () async => CounterCubit(),
      act: (cubit) async => cubit.increment(),
      expect: <int>[1],
    );
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cubit_test: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cubit_test/cubit_test.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
85
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
cubit ^0.1.0 0.1.0
meta ^1.1.6 1.1.8
mockito ^4.0.0 4.1.1
test ^1.14.0 1.15.1
Transitive dependencies
_fe_analyzer_shared 5.0.0
analyzer 0.39.11
args 1.6.0
async 2.4.1
boolean_selector 2.0.0
charcode 1.1.3
collection 1.14.13
convert 2.1.1
coverage 0.14.0
crypto 2.1.5
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
http 0.12.1
http_multi_server 2.2.0
http_parser 3.1.4
io 0.3.4
js 0.6.2
logging 0.11.4
matcher 0.12.8
mime 0.9.6+3
node_interop 1.1.1
node_io 1.1.1
node_preamble 1.4.12
package_config 1.9.3
path 1.7.0
pool 1.4.0
pub_semver 1.4.4
shelf 0.7.7
shelf_packages_handler 2.0.0
shelf_static 0.2.8
shelf_web_socket 0.2.3
source_map_stack_trace 2.0.0
source_maps 0.10.9
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.17
test_core 0.3.9
typed_data 1.2.0
vm_service 4.1.0
watcher 0.9.7+15
web_socket_channel 1.1.0
webkit_inspection_protocol 0.7.3
yaml 2.2.1
Dev dependencies
pedantic ^1.9.0 1.9.0 1.9.1
test_coverage ^0.4.1