Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
generate mocks using mockito code generator
  • Loading branch information
fzyzcjy committed Mar 6, 2021
commit a443a43f74e30ddf598ffa59a66c39f1becb17cd
1 change: 1 addition & 0 deletions flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dev_dependencies:
mockito: ^5.0.0-nullsafety.7
yaml: ^3.0.0 # needed for version match (code and pubspec)
pedantic: ^1.10.0
build_runner: ^1.11.5

dependency_overrides:
sentry:
Expand Down
10 changes: 5 additions & 5 deletions flutter/test/default_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:sentry/sentry.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/sentry_flutter_options.dart';

import 'mocks.dart';
import 'mocks.mocks.dart';

void main() {
const _channel = MethodChannel('sentry_flutter');
Expand Down Expand Up @@ -48,7 +48,7 @@ void main() {
_reportError(exception: exception);

final event = verify(
await fixture.hub.captureEvent(captureAny!),
await fixture.hub.captureEvent(captureAny),
).captured.first as SentryEvent;

expect(SentryLevel.fatal, event.level);
Expand All @@ -67,23 +67,23 @@ void main() {

_reportError(handler: defaultError);

verify(await fixture.hub.captureEvent(captureAny!));
verify(await fixture.hub.captureEvent(captureAny));

expect(true, called);
});

test('FlutterError do not capture if silent error', () async {
_reportError(silent: true);

verifyNever(await fixture.hub.captureEvent(captureAny!));
verifyNever(await fixture.hub.captureEvent(captureAny));
});

test('FlutterError captures if silent error but reportSilentFlutterErrors',
() async {
fixture.options.reportSilentFlutterErrors = true;
_reportError(silent: true);

verify(await fixture.hub.captureEvent(captureAny!));
verify(await fixture.hub.captureEvent(captureAny));
});

test('FlutterError adds integration', () async {
Expand Down
9 changes: 4 additions & 5 deletions flutter/test/mocks.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:mockito/mockito.dart';
import 'package:mockito/annotations.dart';
import 'package:sentry/sentry.dart';

class MockHub extends Mock implements Hub {}

class MockTransport extends Mock implements Transport {}

const fakeDsn = 'https://[email protected]/1234567';

@GenerateMocks([Hub, Transport])
void main() {}
101 changes: 101 additions & 0 deletions flutter/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Mocks generated by Mockito 5.0.0-nullsafety.7 from annotations
// in sentry_flutter/test/mocks.dart.
// Do not manually edit this file.

import 'dart:async' as _i4;

import 'package:mockito/mockito.dart' as _i1;
import 'package:sentry/src/hub.dart' as _i3;
import 'package:sentry/src/protocol/breadcrumb.dart' as _i7;
import 'package:sentry/src/protocol/sentry_event.dart' as _i5;
import 'package:sentry/src/protocol/sentry_id.dart' as _i2;
import 'package:sentry/src/protocol/sentry_level.dart' as _i6;
import 'package:sentry/src/sentry_client.dart' as _i8;
import 'package:sentry/src/transport/transport.dart' as _i9;

// ignore_for_file: comment_references
// ignore_for_file: unnecessary_parenthesis

class _FakeSentryId extends _i1.Fake implements _i2.SentryId {}

class _FakeHub extends _i1.Fake implements _i3.Hub {}

/// A class which mocks [Hub].
///
/// See the documentation for Mockito's code generation for more information.
class MockHub extends _i1.Mock implements _i3.Hub {
MockHub() {
_i1.throwOnMissingStub(this);
}

@override
bool get isEnabled =>
(super.noSuchMethod(Invocation.getter(#isEnabled), returnValue: false)
as bool);
@override
_i2.SentryId get lastEventId =>
(super.noSuchMethod(Invocation.getter(#lastEventId),
returnValue: _FakeSentryId()) as _i2.SentryId);
@override
_i4.Future<_i2.SentryId> captureEvent(_i5.SentryEvent? event,
{dynamic stackTrace, dynamic hint}) =>
(super.noSuchMethod(
Invocation.method(
#captureEvent, [event], {#stackTrace: stackTrace, #hint: hint}),
returnValue:
Future.value(_FakeSentryId())) as _i4.Future<_i2.SentryId>);
@override
_i4.Future<_i2.SentryId> captureException(dynamic throwable,
{dynamic stackTrace, dynamic hint}) =>
(super.noSuchMethod(
Invocation.method(#captureException, [throwable],
{#stackTrace: stackTrace, #hint: hint}),
returnValue: Future.value(_FakeSentryId()))
as _i4.Future<_i2.SentryId>);
@override
_i4.Future<_i2.SentryId> captureMessage(String? message,
{_i6.SentryLevel? level,
String? template,
List<dynamic>? params,
dynamic hint}) =>
(super.noSuchMethod(
Invocation.method(#captureMessage, [
message
], {
#level: level,
#template: template,
#params: params,
#hint: hint
}),
returnValue: Future.value(_FakeSentryId()))
as _i4.Future<_i2.SentryId>);
@override
void addBreadcrumb(_i7.Breadcrumb? crumb, {dynamic hint}) => super
.noSuchMethod(Invocation.method(#addBreadcrumb, [crumb], {#hint: hint}),
returnValueForMissingStub: null);
@override
void bindClient(_i8.SentryClient? client) =>
super.noSuchMethod(Invocation.method(#bindClient, [client]),
returnValueForMissingStub: null);
@override
_i3.Hub clone() => (super.noSuchMethod(Invocation.method(#clone, []),
returnValue: _FakeHub()) as _i3.Hub);
@override
void configureScope(_i3.ScopeCallback? callback) =>
super.noSuchMethod(Invocation.method(#configureScope, [callback]),
returnValueForMissingStub: null);
}

/// A class which mocks [Transport].
///
/// See the documentation for Mockito's code generation for more information.
class MockTransport extends _i1.Mock implements _i9.Transport {
MockTransport() {
_i1.throwOnMissingStub(this);
}

@override
_i4.Future<_i2.SentryId> send(_i5.SentryEvent? event) => (super.noSuchMethod(
Invocation.method(#send, [event]),
returnValue: Future.value(_FakeSentryId())) as _i4.Future<_i2.SentryId>);
}
2 changes: 1 addition & 1 deletion flutter/test/not_initialized_widgets_binding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/sentry_flutter_options.dart';

import 'mocks.dart';
import 'mocks.mocks.dart';

/// Tests that require `WidgetsFlutterBinding.ensureInitialized();` not
/// being called at all.
Expand Down
7 changes: 4 additions & 3 deletions flutter/test/sentry_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:sentry/sentry.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import 'mocks.dart';
import 'mocks.mocks.dart';
import 'sentry_flutter_util.dart';

void main() {
Expand Down Expand Up @@ -73,7 +74,7 @@ void main() {
await Sentry.captureMessage('a message');

final event =
verify(transport.send(captureAny!)).captured.first as SentryEvent;
verify(transport.send(captureAny)).captured.first as SentryEvent;

expect(event.sdk!.integrations.length, 7);
expect(event.sdk!.integrations.contains('loadContextsIntegration'), true);
Expand All @@ -92,7 +93,7 @@ void main() {
await Sentry.captureMessage('a message');

final event =
verify(transport.send(captureAny!)).captured.first as SentryEvent;
verify(transport.send(captureAny)).captured.first as SentryEvent;

expect(event.sdk!.integrations.length, 6);
expect(event.sdk!.integrations.contains('loadContextsIntegration'), false);
Expand All @@ -112,7 +113,7 @@ void main() {
await Sentry.captureMessage('a message');

final event =
verify(transport.send(captureAny!)).captured.first as SentryEvent;
verify(transport.send(captureAny)).captured.first as SentryEvent;

expect(event.sdk!.integrations.length, 6);
expect(event.sdk!.integrations.contains('loadAndroidImageListIntegration'),
Expand Down
2 changes: 1 addition & 1 deletion flutter/test/sentry_navigator_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import 'mocks.dart';
import 'mocks.mocks.dart';

void main() {
group('RouteObserverBreadcrumb', () {
Expand Down
2 changes: 1 addition & 1 deletion flutter/test/widgets_binding_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/sentry_flutter_options.dart';
import 'package:sentry_flutter/src/widgets_binding_observer.dart';

import 'mocks.dart';
import 'mocks.mocks.dart';

void main() {
group('WidgetsBindingObserver', () {
Expand Down