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');
});
}
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);
Another way would be to actually run it in a web environment with
flutter test --platform chrome
You can also add
@TestOn('browser')
at the top of your test file so your tests are only run when the platform is a browser.
Look at TestOn and its README.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With