mockor 1.1.0 copy "mockor: ^1.1.0" to clipboard
mockor: ^1.1.0 copied to clipboard

outdated

Generic mocker method generator for `mockito` or `mocktail` to mock any class just like in the original `mockito`.

example/lib/example.dart

library example;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

// This import is used by the generated mockor file to add Mockito's `@GenerateMocks` annotation.
// This need to be added manually.
import 'package:mockito/annotations.dart';

// <file_name>.mocks.dart will be generated by Mockito which contain all the generated mocks.
// This needs to be added manually.
import 'example.mocks.dart';

import 'package:mockor/mockor.dart';

part 'example.mockor.dart';

abstract class ExampleUseCase {
  int exampleInt(int i);
}

abstract class ExampleUseCase2 {
  int? exampleNullableInt();
  void exampleVoid();
}

@GenerateMocker([
  ExampleUseCase,
  ExampleUseCase2,
])
T mock<T extends Object>({bool relaxed = false}) => _$mock<T>(relaxed: relaxed);

void main() {
  late ExampleUseCase exampleUseCase;
  late ExampleUseCase2 exampleUseCase2;

  setUp(() {
    // this will return [MockExampleUseCase]
    exampleUseCase = mock();
    exampleUseCase2 = mock();
  });
  test("given exampleNullableInt throws an exception then don't catch it", () {
    when(exampleUseCase2.exampleNullableInt()).thenThrow(Exception());
    try {
      exampleUseCase2.exampleNullableInt();
      fail('expected exception');
    } on Exception {}
  });
  test('given exampleInt with any param returns 2 then return 2', () {
    /**
     * By default an `asMock` extension method will be generated for all [GenerateMocker.types]
     * which casts it as generated mocked type (MockExampleUseCase).
     * Due to null safety we can only use the [any] matcher on non null params when using the mocked type.
     * Please read Mockito's [Null Safety README](https://github.com/dart-lang/mockito/blob/master/NULL_SAFETY_README.md) for more info.
     */
    when(exampleUseCase.asMock().exampleInt(any)).thenReturn(2);
    expect(exampleUseCase.exampleInt(1), 2);
  });
  test("given relaxed is true then return null on non null method not stubbed",
      () {
    final ExampleUseCase2 useCaseRelaxed = mock(relaxed: true);
    try {
      useCaseRelaxed.exampleNullableInt();
    } on MissingStubError {
      fail("did not expect $MissingStubError");
    }
  });

  //NOTE: this test only succeeds in `mockito` and not in `mocktail`
  test(
      "given relaxed is false then don't throw exception on void method not stubbed",
      () {
    final ExampleUseCase2 useCase = mock(relaxed: false);
    try {
      useCase.exampleVoid();
    } on MissingStubError {
      fail("did not $MissingStubError");
    }
  });
}
1
likes
0
pub points
0%
popularity

Publisher

verified publisherrmdy.be

Generic mocker method generator for `mockito` or `mocktail` to mock any class just like in the original `mockito`.

Homepage

License

unknown (license)

Dependencies

analyzer, build, code_builder, dart_style, flutter, path, source_gen

More

Packages that depend on mockor