Skip to content

Commit

Permalink
Merge pull request #2 from GJJ2019/add_test_cases
Browse files Browse the repository at this point in the history
Adding plugin test cases
  • Loading branch information
GJJ2019 authored Jul 11, 2022
2 parents fb053ef + 2ba8610 commit f91641f
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/src/model/easy_upi_payment_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class EasyUpiPaymentModel {
EasyUpiPaymentModel({
const EasyUpiPaymentModel({
required this.payeeVpa,
required this.payeeName,
required this.amount,
Expand Down
10 changes: 10 additions & 0 deletions lib/src/model/transaction_detail_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ class TransactionDetailModel {
String toString() {
return 'TransactionDetailModel(transactionId: $transactionId, responseCode: $responseCode, approvalRefNo: $approvalRefNo, transactionRefId: $transactionRefId, amount: $amount)';
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'transactionId': transactionId,
'responseCode': responseCode,
'approvalRefNo': approvalRefNo,
'transactionRefId': transactionRefId,
'amount': amount,
};
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dev_dependencies:
flutter_test:
sdk: flutter
lint: ^1.8.2
mockito: ^5.2.0
build_runner: ^2.1.11

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
61 changes: 43 additions & 18 deletions test/easy_upi_payment_method_channel_test.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import 'package:easy_upi_payment/easy_upi_payment.dart';
import 'package:easy_upi_payment/src/easy_upi_payment_method_channel.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import 'fake_data.dart';

void main() {
// MethodChannelEasyUpiPayment platform = MethodChannelEasyUpiPayment();
// const MethodChannel channel = MethodChannel('easy_upi_payment');
//
// TestWidgetsFlutterBinding.ensureInitialized();
//
// setUp(() {
// channel.setMockMethodCallHandler((MethodCall methodCall) async {
// return '42';
// });
// });
//
// tearDown(() {
// channel.setMockMethodCallHandler(null);
// });
//
// test('getPlatformVersion', () async {
// expect(await platform.getPlatformVersion(), '42');
// });
final MethodChannelEasyUpiPayment platform = MethodChannelEasyUpiPayment();
const MethodChannel channel = MethodChannel('easy_upi_payment');

TestWidgetsFlutterBinding.ensureInitialized();

tearDown(() {
channel.setMockMethodCallHandler(null);
});

group('startPayment with appropriate result', () {
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return fakeTransactionDetailsModel.toMap();
});
});

test('startPayment', () async {
expect(await platform.startPayment(fakeEasyUpiPaymentModel), fakeTransactionDetailsModel);
});
});

group('startPayment with exception', () {
final platformException = PlatformException(code: EasyUpiPaymentExceptionType.failedException.toString());
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return platformException;
});
});

test('startPayment', () async {
expect(
() => platform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});
});
}
105 changes: 76 additions & 29 deletions test/easy_upi_payment_test.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,76 @@
// import 'package:flutter_test/flutter_test.dart';
// import 'package:easy_upi_payment/easy_upi_payment.dart';
// import 'package:easy_upi_payment/easy_upi_payment_platform_interface.dart';
// import 'package:easy_upi_payment/easy_upi_payment_method_channel.dart';
// import 'package:plugin_platform_interface/plugin_platform_interface.dart';
//
// class MockEasyUpiPaymentPlatform
// with MockPlatformInterfaceMixin
// implements EasyUpiPaymentPlatform {
//
// @override
// Future<String?> getPlatformVersion() => Future.value('42');
// }
//
// void main() {
// final EasyUpiPaymentPlatform initialPlatform = EasyUpiPaymentPlatform.instance;
//
// test('$MethodChannelEasyUpiPayment is the default instance', () {
// expect(initialPlatform, isInstanceOf<MethodChannelEasyUpiPayment>());
// });
//
// test('getPlatformVersion', () async {
// EasyUpiPayment easyUpiPaymentPlugin = EasyUpiPayment();
// MockEasyUpiPaymentPlatform fakePlatform = MockEasyUpiPaymentPlatform();
// EasyUpiPaymentPlatform.instance = fakePlatform;
//
// expect(await easyUpiPaymentPlugin.getPlatformVersion(), '42');
// });
// }
import 'package:easy_upi_payment/easy_upi_payment.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'easy_upi_payment_test.mocks.dart';
import 'fake_data.dart';

@GenerateMocks([EasyUpiPaymentPlatform])
void main() {
final mockPlatform = MockEasyUpiPaymentPlatform();

test('startPayment', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenAnswer(
(_) => Future.value(fakeTransactionDetailsModel),
);
expect(await mockPlatform.startPayment(fakeEasyUpiPaymentModel), fakeTransactionDetailsModel);
});

test('startPayment with cancelled Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.cancelledException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});

test('startPayment with failed Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.failedException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});

test('startPayment with submitted Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.submittedException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});

test('startPayment with unknown Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.unknownException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});
}
40 changes: 40 additions & 0 deletions test/easy_upi_payment_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Mocks generated by Mockito 5.2.0 from annotations
// in easy_upi_payment/test/easy_upi_payment_test.dart.
// Do not manually edit this file.

import 'dart:async' as _i3;

import 'package:easy_upi_payment/src/easy_upi_payment_platform_interface.dart'
as _i2;
import 'package:easy_upi_payment/src/model/easy_upi_payment_model.dart' as _i5;
import 'package:easy_upi_payment/src/model/transaction_detail_model.dart'
as _i4;
import 'package:mockito/mockito.dart' as _i1;

// 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

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

@override
_i3.Future<_i4.TransactionDetailModel?> startPayment(
_i5.EasyUpiPaymentModel? easyUpiPaymentModel) =>
(super.noSuchMethod(
Invocation.method(#startPayment, [easyUpiPaymentModel]),
returnValue: Future<_i4.TransactionDetailModel?>.value())
as _i3.Future<_i4.TransactionDetailModel?>);
}
16 changes: 16 additions & 0 deletions test/fake_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:easy_upi_payment/easy_upi_payment.dart';

const fakeTransactionDetailsModel = TransactionDetailModel(
transactionId: 'transactionId',
responseCode: 'responseCode',
approvalRefNo: 'approvalRefNo',
transactionRefId: 'transactionRefId',
amount: 'amount',
);

const fakeEasyUpiPaymentModel = EasyUpiPaymentModel(
payeeVpa: 'payeeVpa',
payeeName: 'payeeName',
amount: 10.0,
description: 'description',
);

0 comments on commit f91641f

Please sign in to comment.