Jonny Borges

Get.replace on tests

1 import 'dart:io'; 1 import 'dart:io';
2 -import 'dart:math';  
3 2
4 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
5 import 'package:flutter_test/flutter_test.dart'; 4 import 'package:flutter_test/flutter_test.dart';
@@ -11,12 +10,9 @@ import 'package:get_demo/pages/home/presentation/controllers/home_controller.dar @@ -11,12 +10,9 @@ import 'package:get_demo/pages/home/presentation/controllers/home_controller.dar
11 // import 'package:get_test/get_test.dart'; 10 // import 'package:get_test/get_test.dart';
12 import 'package:matcher/matcher.dart' as m; 11 import 'package:matcher/matcher.dart' as m;
13 12
14 -class MockRepository implements IHomeRepository { 13 +class MockRepositorySuccess implements IHomeRepository {
15 @override 14 @override
16 Future<CasesModel> getCases() async { 15 Future<CasesModel> getCases() async {
17 - await Future.delayed(Duration(milliseconds: 100));  
18 -  
19 - if (Random().nextBool()) {  
20 return CasesModel( 16 return CasesModel(
21 global: Global( 17 global: Global(
22 totalDeaths: 100, 18 totalDeaths: 100,
@@ -32,7 +28,11 @@ class MockRepository implements IHomeRepository { @@ -32,7 +28,11 @@ class MockRepository implements IHomeRepository {
32 message: '', 28 message: '',
33 ); 29 );
34 } 30 }
  31 +}
35 32
  33 +class MockRepositoryFailure implements IHomeRepository {
  34 + @override
  35 + Future<CasesModel> getCases() async {
36 return Future<CasesModel>.error('error'); 36 return Future<CasesModel>.error('error');
37 } 37 }
38 } 38 }
@@ -41,28 +41,18 @@ void main() { @@ -41,28 +41,18 @@ void main() {
41 WidgetsFlutterBinding.ensureInitialized(); 41 WidgetsFlutterBinding.ensureInitialized();
42 setUpAll(() => HttpOverrides.global = null); 42 setUpAll(() => HttpOverrides.global = null);
43 final binding = BindingsBuilder(() { 43 final binding = BindingsBuilder(() {
44 - Get.lazyPut<IHomeRepository>(() => MockRepository()); 44 + Get.lazyPut<IHomeRepository>(() => MockRepositorySuccess());
45 Get.lazyPut<HomeController>( 45 Get.lazyPut<HomeController>(
46 - () => HomeController(homeRepository: Get.find())); 46 + () => HomeController(homeRepository: Get.find()),
  47 + );
47 }); 48 });
48 49
49 - test('Test Binding', () {  
50 - expect(Get.isPrepared<HomeController>(), false);  
51 - expect(Get.isPrepared<IHomeRepository>(), false);  
52 -  
53 - /// test you Binding class with BindingsBuilder  
54 - binding.builder();  
55 -  
56 - expect(Get.isPrepared<HomeController>(), true);  
57 - expect(Get.isPrepared<IHomeRepository>(), true);  
58 -  
59 - Get.reset();  
60 - });  
61 test('Test Controller', () async { 50 test('Test Controller', () async {
62 /// Controller can't be on memory 51 /// Controller can't be on memory
63 - expect(() => Get.find<HomeController>(), throwsA(m.TypeMatcher<String>())); 52 + expect(() => Get.find<HomeController>(tag: 'success'),
  53 + throwsA(m.TypeMatcher<String>()));
64 54
65 - /// build Binding 55 + /// binding will put the controller on memory
66 binding.builder(); 56 binding.builder();
67 57
68 /// recover your controller 58 /// recover your controller
@@ -77,24 +67,15 @@ void main() { @@ -77,24 +67,15 @@ void main() {
77 /// await time request 67 /// await time request
78 await Future.delayed(Duration(milliseconds: 100)); 68 await Future.delayed(Duration(milliseconds: 100));
79 69
80 - if (controller.status.isError) {  
81 - expect(controller.state, null);  
82 - }  
83 -  
84 - if (controller.status.isSuccess) { 70 + /// test if status is success
  71 + expect(controller.status.isSuccess, true);
85 expect(controller.state.global.totalDeaths, 100); 72 expect(controller.state.global.totalDeaths, 100);
86 expect(controller.state.global.totalConfirmed, 200); 73 expect(controller.state.global.totalConfirmed, 200);
87 - }  
88 - });  
89 74
90 - test('ever', () async {  
91 - final count = ''.obs;  
92 - var result = '';  
93 - ever<String>(count, (value) {  
94 - result = value;  
95 - });  
96 - count.value = '1';  
97 - expect('1', result); 75 + /// test if status is error
  76 + Get.lazyReplace<IHomeRepository>(() => MockRepositoryFailure());
  77 + expect(controller.status.isError, true);
  78 + expect(controller.state, null);
98 }); 79 });
99 80
100 /// Tests with GetTests 81 /// Tests with GetTests
@@ -151,26 +132,3 @@ void main() { @@ -151,26 +132,3 @@ void main() {
151 }, 132 },
152 );*/ 133 );*/
153 } 134 }
154 -  
155 -class Controller extends GetxController {  
156 - final count = 0.obs;  
157 - void increment() => count.value++;  
158 -  
159 - @override  
160 - void onInit() {  
161 - print('inittt');  
162 - super.onInit();  
163 - }  
164 -  
165 - @override  
166 - void onReady() {  
167 - print('onReady');  
168 - super.onReady();  
169 - }  
170 -  
171 - @override  
172 - void onClose() {  
173 - super.onClose();  
174 - print('onClose');  
175 - }  
176 -}