かなり苦労しましたが、こんな感じで実装可能です
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:mockito/mockito.dart';
// その他mockとかimportしてるファイルありますが、割愛しました
void main() {
WidgetsFlutterBinding.ensureInitialized();
GroupRepository repository;
MockHttpClient httpClient;
FirebaseAuth firebase;
DioAdapterMockito dioAdapterMockito;
Dio dio;
setUp(() async {
dio = Dio();
dioAdapterMockito = DioAdapterMockito();
dio.httpClientAdapter = dioAdapterMockito;
httpClient = MockHttpClient(dio: dio);
firebase = MockFirebaseAuth();
await firebase.signInWithEmailAndPassword(
email: 'test@gmail.com',
password: '11111111',
);
repository = GroupRepository(
httpClient: httpClient,
fb: firebase,
);
});
test('findAll()', () async {
const body = [
{'id': 1, 'name': '青年会議所', 'description': 'test'},
{'id': 2, 'name': 'Vertech', 'description': 'test'}
];
final responsePayload = jsonEncode(body);
final responseBody = ResponseBody.fromString(
responsePayload,
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
);
when(dioAdapterMockito.fetch(any, any, any))
.thenAnswer((_) async => responseBody);
final groups = await repository.findAll();
expect(groups.length, 2);
expect(groups[0].name, '青年会議所');
expect(groups[0].description, 'test');
expect(groups[1].name, 'Vertech');
expect(groups[1].description, 'test');
});
}
コメントを残す