main_test.dart
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart';
import 'package:get_demo/pages/home/domain/entity/cases_model.dart';
import 'package:get_demo/pages/home/presentation/controllers/home_controller.dart';
// import 'package:get_demo/routes/app_pages.dart';
// import 'package:get_test/get_test.dart';
class MockRepositorySuccess implements IHomeRepository {
@override
Future<CasesModel> getCases() async {
return CasesModel(
global: Global(
totalDeaths: 100,
totalConfirmed: 200,
date: DateTime.now(),
newConfirmed: 0,
newDeaths: 0,
newRecovered: 0,
totalRecovered: 0),
countries: [],
date: DateTime.now(),
message: '',
);
}
}
class MockRepositoryFailure implements IHomeRepository {
@override
Future<CasesModel> getCases() async {
return Future<CasesModel>.error('error');
}
}
class MockBinding extends Binding {
@override
List<Bind> dependencies() {
return [
Bind.lazyPut<IHomeRepository>(() => MockRepositorySuccess()),
Bind.lazyPut<HomeController>(
() => HomeController(homeRepository: Get.find()),
)
];
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
setUpAll(() => HttpOverrides.global = null);
final binding = MockBinding();
test('Test Controller', () async {
/// Controller can't be on memory
expect(() => Get.find<HomeController>(tag: 'success'),
throwsA(const TypeMatcher<String>()));
/// binding will put the controller on memory
binding.dependencies();
/// recover your controller
final controller = Get.find<HomeController>();
/// check if onInit was called
expect(controller.initialized, true);
/// check initial Status
expect(controller.status.isLoading, true);
/// await time request
await Future.delayed(const Duration(milliseconds: 100));
/// test if status is success
expect(controller.status.isSuccess, true);
expect(controller.state.global.totalDeaths, 100);
expect(controller.state.global.totalConfirmed, 200);
/// test if status is error
Get.lazyReplace<IHomeRepository>(() => MockRepositoryFailure());
expect(controller.status.isError, true);
expect(controller.state, null);
});
/// Tests with GetTests
/// TEMPORARILY REMOVED from the null-safetym branch as
/// get_test is not yet null safety.
/* getTest(
"test description",
getPages: AppPages.routes,
initialRoute: AppPages.INITIAL,
widgetTest: (tester) async {
expect('/home', Get.currentRoute);
Get.toNamed('/home/country');
expect('/home/country', Get.currentRoute);
Get.toNamed('/home/country/details');
expect('/home/country/details', Get.currentRoute);
Get.back();
expect('/home/country', Get.currentRoute);
},
);
testGetX(
'GetX test',
widget: GetX<Controller>(
init: Controller(),
builder: (controller) {
return Text("ban:${controller.count}");
},
),
test: (e) {
expect(find.text("ban:0"), findsOneWidget);
},
);
testController<Controller>(
'Controller test',
(controller) {
print('controllllllll ${controller.count}');
},
controller: Controller(),
onInit: (c) {
c.increment();
print('onInit');
},
onReady: (c) {
print('onReady');
c.increment();
},
onClose: (c) {
print('onClose');
},
);*/
}