Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/nice mocks #17

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.10.5",
"flavors": {}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ pubspec.lock
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
pubspec.lock

# FVM
.fvm/flutter_sdk
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.3.0

- Update to Dart 3.0
- Bump minimal `analyzer` version to 5.0.0
- Add mockito nice mocks support
- Fix `UnimplementedError` when resolved mock method type is the mock variant

## 1.2.1

- Update README
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ analyzer:
```

## Troubleshooting
### "Error, a mock class for 'MockExampleUseCase' has not been generated yet."

The mocker method only supports returning a mock instance for it's base type.

### Error when using `any`: "The argument type 'Null' can't be assigned to the parameter type..."
To be able to use of `any`, you need the mocked type and not the base type.
Expand Down
8 changes: 4 additions & 4 deletions example/lib/example.mockor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: non_constant_identifier_names

part of example;
part of 'example.dart';

// **************************************************************************
// MockerGenerator
// **************************************************************************

@GenerateMocks([], customMocks: [
@GenerateNiceMocks([
MockSpec<ExampleUseCase>(
as: #MockExampleUseCase,
returnNullOnMissingStub: true,
),
MockSpec<ExampleUseCase2>(
as: #MockExampleUseCase2,
returnNullOnMissingStub: true,
),
])
dynamic _$mock<T extends Object>({bool? relaxed}) {
relaxed ??= false;
switch (T) {
case ExampleUseCase:
case MockExampleUseCase:
final mock = MockExampleUseCase();
if (!relaxed) {
throwOnMissingStub(mock);
}
return mock;
case ExampleUseCase2:
case MockExampleUseCase2:
final mock = MockExampleUseCase2();
if (!relaxed) {
throwOnMissingStub(mock);
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: example

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: '>=3.0.0 <4.0.0'

dependencies:
flutter:
sdk: flutter
analyzer: '>=2.0.0 <4.0.0'
analyzer: ^5.0.0


dev_dependencies:
Expand Down
16 changes: 7 additions & 9 deletions example/test/example_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:mockito/annotations.dart';
import 'example_test.mocks.dart';
import 'package:mockito/mockito.dart';
import 'package:mockor/mockor.dart';

import 'example_test.mocks.dart';
import 'models/model_a.dart' as ModelA;
import 'models/model_b.dart' as ModelB;

part 'example_test.mockor.dart';

abstract class ExampleUseCase {
Expand All @@ -30,11 +31,10 @@ class ExampleUseCase3 {}
ModelA.Model,
ModelB.Model,
])
T _mock<T extends Object>({bool relaxed = false}) =>
_$_mock<T>(relaxed: relaxed);
T _mock<T extends Object>({bool relaxed = false}) => _$_mock<T>(relaxed: relaxed);
void main() {
late ExampleUseCase exampleUseCase;
late ExampleUseCase2 exampleUseCase2;
late MockExampleUseCase2 exampleUseCase2;
// ignore: unused_local_variable
late ModelA.Model modelA;
// ignore: unused_local_variable
Expand Down Expand Up @@ -81,8 +81,7 @@ void main() {
final ExampleUseCase2 useCase = _mock(relaxed: true);
useCase..exampleVoid();
});
test("then don't throw exception on Future<void> method not stubbed",
() async {
test("then don't throw exception on Future<void> method not stubbed", () async {
final ExampleUseCase2 useCase = _mock(relaxed: true);
try {
await useCase.exampleFutureVoid();
Expand Down Expand Up @@ -117,8 +116,7 @@ void main() {
}
});

test("then don't throw exception on Future<void> method not stubbed",
() async {
test("then don't throw exception on Future<void> method not stubbed", () async {
final ExampleUseCase2 useCase = _mock(relaxed: false);
try {
await useCase.exampleFutureVoid();
Expand Down
10 changes: 5 additions & 5 deletions example/test/example_test.mockor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ part of 'example_test.dart';
// MockerGenerator
// **************************************************************************

@GenerateMocks([], customMocks: [
@GenerateNiceMocks([
MockSpec<ExampleUseCase>(
as: #MockExampleUseCase,
returnNullOnMissingStub: true,
),
MockSpec<ExampleUseCase2>(
as: #MockExampleUseCase2,
returnNullOnMissingStub: true,
),
MockSpec<ModelA.Model>(
as: #MockModelAModel,
returnNullOnMissingStub: true,
),
MockSpec<ModelB.Model>(
as: #MockModelBModel,
returnNullOnMissingStub: true,
),
])
dynamic _$_mock<T extends Object>({bool? relaxed}) {
relaxed ??= false;
switch (T) {
case ExampleUseCase:
case MockExampleUseCase:
final mock = MockExampleUseCase();
if (!relaxed) {
throwOnMissingStub(mock);
}
return mock;
case ExampleUseCase2:
case MockExampleUseCase2:
final mock = MockExampleUseCase2();
if (!relaxed) {
throwOnMissingStub(mock);
}
return mock;
case ModelA.Model:
case MockModelAModel:
final mock = MockModelAModel();
if (!relaxed) {
throwOnMissingStub(mock);
}
return mock;
case ModelB.Model:
case MockModelBModel:
final mock = MockModelBModel();
if (!relaxed) {
throwOnMissingStub(mock);
Expand Down
81 changes: 53 additions & 28 deletions example/test/example_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Mocks generated by Mockito 5.0.15 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in example/test/example_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i3;

import 'package:mockito/mockito.dart' as _i1;
Expand All @@ -10,62 +13,84 @@ import 'example_test.dart' as _i2;
import 'models/model_a.dart' as _i4;
import 'models/model_b.dart' as _i5;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

/// A class which mocks [ExampleUseCase].
///
/// See the documentation for Mockito's code generation for more information.
class MockExampleUseCase extends _i1.Mock implements _i2.ExampleUseCase {
@override
int exampleInt(_i2.ExampleModel? model) => (super
.noSuchMethod(Invocation.method(#exampleInt, [model]), returnValue: 0)
as int);
@override
String toString() => super.toString();
int exampleInt(_i2.ExampleModel? model) => (super.noSuchMethod(
Invocation.method(
#exampleInt,
[model],
),
returnValue: 0,
returnValueForMissingStub: 0,
) as int);
}

/// A class which mocks [ExampleUseCase2].
///
/// See the documentation for Mockito's code generation for more information.
class MockExampleUseCase2 extends _i1.Mock implements _i2.ExampleUseCase2 {
@override
void exampleVoid() => super.noSuchMethod(Invocation.method(#exampleVoid, []),
returnValueForMissingStub: null);
void exampleVoid() => super.noSuchMethod(
Invocation.method(
#exampleVoid,
[],
),
returnValueForMissingStub: null,
);
@override
_i3.Future<void> exampleFutureVoid() =>
(super.noSuchMethod(Invocation.method(#exampleFutureVoid, []),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i3.Future<void>);
_i3.Future<void> exampleFutureVoid() => (super.noSuchMethod(
Invocation.method(
#exampleFutureVoid,
[],
),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
) as _i3.Future<void>);
@override
void operator [](String? key) =>
super.noSuchMethod(Invocation.method(#[], [key]),
returnValueForMissingStub: null);
void operator [](String? key) => super.noSuchMethod(
Invocation.method(
#[],
[key],
),
returnValueForMissingStub: null,
);
@override
void operator []=(String? key, String? value) =>
super.noSuchMethod(Invocation.method(#[]=, [key, value]),
returnValueForMissingStub: null);
@override
String toString() => super.toString();
void operator []=(
String? key,
String? value,
) =>
super.noSuchMethod(
Invocation.method(
#[]=,
[
key,
value,
],
),
returnValueForMissingStub: null,
);
}

/// A class which mocks [Model].
///
/// See the documentation for Mockito's code generation for more information.
class MockModelAModel extends _i1.Mock implements _i4.Model {
@override
String toString() => super.toString();
}
class MockModelAModel extends _i1.Mock implements _i4.Model {}

/// A class which mocks [Model].
///
/// See the documentation for Mockito's code generation for more information.
class MockModelBModel extends _i1.Mock implements _i5.Model {
@override
String toString() => super.toString();
}
class MockModelBModel extends _i1.Mock implements _i5.Model {}
25 changes: 10 additions & 15 deletions example/test/mocker_with_custom_mockito_annotation_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'mocker_with_custom_mockito_annotation_test.mocks.dart';
import 'package:mockor/mockor.dart';

import 'mocker_with_custom_mockito_annotation_test.mocks.dart';

part 'mocker_with_custom_mockito_annotation_test.mockor.dart';

abstract class MockerWithCustomMockitoUseCase {
Expand All @@ -13,21 +15,15 @@ abstract class MockerWithCustomMockitoUseCase2 {
void test();
}

@GenerateMocker([MockerWithCustomMockitoUseCase],
generateMockitoAnnotation: false)
@GenerateMocks([
MockerWithCustomMockitoUseCase,
MockerWithCustomMockitoUseCase2
], customMocks: [
MockSpec<MockerWithCustomMockitoUseCase>(
as: #MockerWithCustomMockitoUseCaseRelaxed, returnNullOnMissingStub: true)
@GenerateMocker([MockerWithCustomMockitoUseCase], generateMockitoAnnotation: false)
@GenerateNiceMocks([
MockSpec<MockerWithCustomMockitoUseCase>(),
MockSpec<MockerWithCustomMockitoUseCase>(as: #MockMockerWithCustomMockitoUseCaseRelaxed)
])
T _mock<T extends Object>({bool relaxed = false}) =>
_$_mock<T>(relaxed: relaxed);
T _mock<T extends Object>({bool relaxed = false}) => _$_mock<T>(relaxed: relaxed);

void main() {
test(
"given MockerWithCustomMockitoUseCase type is provided to both annotations, it can be found",
test("given MockerWithCustomMockitoUseCase type is provided to both annotations, it can be found",
() {
try {
MockerWithCustomMockitoUseCase useCase1 = _mock();
Expand All @@ -46,8 +42,7 @@ void main() {
} on UnimplementedError {}
});
test("MockerWithCustomMockitoUseCaseRelaxed gets generated", () {
MockerWithCustomMockitoUseCase useCase =
MockerWithCustomMockitoUseCaseRelaxed();
MockerWithCustomMockitoUseCase useCase = MockMockerWithCustomMockitoUseCaseRelaxed();
useCase.test();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dynamic _$_mock<T extends Object>({bool? relaxed}) {
relaxed ??= false;
switch (T) {
case MockerWithCustomMockitoUseCase:
case MockMockerWithCustomMockitoUseCase:
final mock = MockMockerWithCustomMockitoUseCase();
if (!relaxed) {
throwOnMissingStub(mock);
Expand Down
Loading