Skip to content

Commit

Permalink
feat: log in error test
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Jul 7, 2024
1 parent 1772e05 commit 465a22c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
17 changes: 15 additions & 2 deletions packages/alice_dio/test/alice_dio_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:alice/model/alice_from_data_field.dart';
import 'package:alice/model/alice_http_call.dart';
import 'package:alice/model/alice_http_error.dart';
import 'package:alice/model/alice_http_response.dart';
import 'package:alice/model/alice_log.dart';
import 'package:alice_dio/alice_dio_adapter.dart';
import 'package:alice_test/alice_test.dart';
import 'package:dio/dio.dart';
Expand All @@ -24,11 +25,13 @@ void main() {
registerFallbackValue(AliceHttpCall(0));
registerFallbackValue(AliceHttpResponse());
registerFallbackValue(AliceHttpError());
registerFallbackValue(AliceLog(message: ''));

aliceCore = AliceCoreMock();
when(() => aliceCore.addCall(any())).thenAnswer((_) => {});
when(() => aliceCore.addResponse(any(), any())).thenAnswer((_) => {});
when(() => aliceCore.addError(any(), any())).thenAnswer((_) => {});
when(() => aliceCore.addLog(any())).thenAnswer((_) => {});

aliceDioAdapter = AliceDioAdapter();
aliceDioAdapter.injectCore(aliceCore);
Expand Down Expand Up @@ -276,9 +279,19 @@ void main() {

verify(() => aliceCore.addResponse(any(that: nextResponseMatcher), any()));

final errorMatcher =
buildErrorMatcher(checkError: true, checkStacktrace: true);
final errorMatcher = buildErrorMatcher(
checkError: true,
checkStacktrace: true,
);

verify(() => aliceCore.addError(any(that: errorMatcher), any()));

final logMatcher = buildLogMatcher(
checkMessage: true,
checkError: true,
checkStacktrace: true,
checkTime: true,
);
verify(() => aliceCore.addLog(any(that: logMatcher)));
});
}
1 change: 1 addition & 0 deletions packages/alice_test/lib/alice_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'matcher/alice_call_matcher.dart';
export 'matcher/alice_request_matcher.dart';
export 'matcher/alice_response_matcher.dart';
export 'matcher/alice_error_matcher.dart';
export 'matcher/alice_log_matcher.dart';
6 changes: 4 additions & 2 deletions packages/alice_test/lib/matcher/alice_error_matcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ TypeMatcher<AliceHttpError> buildErrorMatcher(
{bool? checkError, bool? checkStacktrace}) {
var matcher = const TypeMatcher<AliceHttpError>();
if (checkError == true) {
matcher = matcher.having((error) => error.error, "error", isNotEmpty);
matcher =
matcher.having((error) => error.error.toString(), "error", isNotEmpty);
}
if (checkStacktrace == true) {
matcher = matcher.having((error) => error.error, "stackTrace", isNotEmpty);
matcher = matcher.having(
(error) => error.stackTrace.toString(), "stackTrace", isNotEmpty);
}

return matcher;
Expand Down
27 changes: 27 additions & 0 deletions packages/alice_test/lib/matcher/alice_log_matcher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:alice/model/alice_log.dart';
import 'package:test/test.dart';

TypeMatcher<AliceLog> buildLogMatcher({
bool? checkMessage,
bool? checkError,
bool? checkStacktrace,
bool? checkTime,
}) {
var matcher = const TypeMatcher<AliceLog>();
if (checkMessage != null) {
matcher = matcher.having((log) => log.message, "message", isNotEmpty);
}
if (checkError == true) {
matcher =
matcher.having((log) => log.error.toString(), "error", isNotEmpty);
}
if (checkStacktrace == true) {
matcher = matcher.having(
(log) => log.stackTrace.toString(), "stackTrace", isNotEmpty);
}
if (checkTime == true) {
matcher = matcher.having(
(log) => log.timestamp.millisecondsSinceEpoch, "timestamp", isPositive);
}
return matcher;
}

0 comments on commit 465a22c

Please sign in to comment.