diff --git a/dart/test/mocks/mock_transport.dart b/dart/test/mocks/mock_transport.dart index ff1a611c72..fa6e033935 100644 --- a/dart/test/mocks/mock_transport.dart +++ b/dart/test/mocks/mock_transport.dart @@ -30,7 +30,7 @@ class MockTransport implements Transport { final event = await _eventFromEnvelope(envelope); events.add(event); } catch (e, stack) { - _exceptions += "$e\n$stack\n\n"; + _exceptions += '$e\n$stack\n\n'; rethrow; } diff --git a/dio/test/mocks/mock_transport.dart b/dio/test/mocks/mock_transport.dart index 5b9b13de03..fa6e033935 100644 --- a/dio/test/mocks/mock_transport.dart +++ b/dio/test/mocks/mock_transport.dart @@ -1,12 +1,19 @@ import 'dart:convert'; import 'package:sentry/sentry.dart'; -import 'no_such_method_provider.dart'; +import 'package:test/expect.dart'; -class MockTransport with NoSuchMethodProvider implements Transport { +class MockTransport implements Transport { List envelopes = []; List events = []; - int calls = 0; + + int _calls = 0; + String _exceptions = ''; + + int get calls { + expect(_exceptions, isEmpty); + return _calls; + } bool called(int calls) { return calls == calls; @@ -14,11 +21,18 @@ class MockTransport with NoSuchMethodProvider implements Transport { @override Future send(SentryEnvelope envelope) async { - calls++; - - envelopes.add(envelope); - final event = await _eventFromEnvelope(envelope); - events.add(event); + _calls++; + + // Exception here would be swallowed by Sentry, making it hard to find test + // failure causes. Instead, we log them and check on access to [calls]. + try { + envelopes.add(envelope); + final event = await _eventFromEnvelope(envelope); + events.add(event); + } catch (e, stack) { + _exceptions += '$e\n$stack\n\n'; + rethrow; + } return envelope.header.eventId ?? SentryId.empty(); } @@ -28,36 +42,14 @@ class MockTransport with NoSuchMethodProvider implements Transport { envelopeItemData.addAll(await envelope.items.first.envelopeItemStream()); final envelopeItem = utf8.decode(envelopeItemData); - final dynamic envelopeItemJson = jsonDecode(envelopeItem.split('\n').last); - final envelopeMap = envelopeItemJson as Map; - final requestJson = envelopeMap['request'] as Map?; - - // '_InternalLinkedHashMap' is not a subtype of type 'Map' - final headersMap = requestJson?['headers'] as Map?; - final newHeadersMap = {}; - if (headersMap != null) { - for (final entry in headersMap.entries) { - newHeadersMap[entry.key] = entry.value as String; - } - envelopeMap['request']['headers'] = newHeadersMap; - } - - final otherMap = requestJson?['other'] as Map?; - final newOtherMap = {}; - if (otherMap != null) { - for (final entry in otherMap.entries) { - newOtherMap[entry.key] = entry.value as String; - } - envelopeMap['request']['other'] = newOtherMap; - } - - return SentryEvent.fromJson(envelopeMap); + final envelopeItemJson = jsonDecode(envelopeItem.split('\n').last); + return SentryEvent.fromJson(envelopeItemJson); } void reset() { envelopes.clear(); events.clear(); - calls = 0; + _calls = 0; } }