Issue
in my code i have a lookup
method:
lookup.dart
Future<http.Response> httpLookup(String address) {
return kIsWeb
? _httpClient.get(address)
: _httpClient.get(
Uri.https(address, ''),
);
}
how can i test the kIsWeb
constant during unit testing? this is what i have tried so far but the coverage is not going though.
lookup_test.dart
@TestOn('browser')
void main (){
test('shoud test lookup', () {
InternetLookup lookup = InternetLookup();
when(mockInternetLookup.httpLookup(any))
.thenAnswer((realInvocation) async => http.Response('success', 200));
lookup.httpLookup('www.google.com');
});
}
Solution
You can to use an Interface and to mock it.
abstract class IAppService {
bool getkIsWeb();
}
class AppService implements IAppService {
bool getkIsWeb() {
return kIsWeb;
}
}
In the tests, you must to use like as
class MockAppService extends Mock implements IAppService {}
...
when(appService.getkIsWeb())
.thenAnswer((realInvocation) => true);
Answered By - Filipe Piletti Plucenio
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.