Created
August 7, 2020 18:24
-
-
Save agnamc9/8bc94c2005c9acace2617ce762e61b75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Here debug console output | |
Expected: <Instance of 'DataResponse<Utilisateur>'> | |
Actual: <null> | |
package:test_api expect | |
package:flutter_test/src/widget_tester.dart 369:3 expect | |
test/repositories/login/login_api_client_test.dart 30:7 main.<fn>.<fn> | |
// login_api_client_test.dart | |
class MockNetworkUtil extends Mock implements NetworkUtil<Utilisateur> {} | |
main() { | |
group('LoginApiClient', () { | |
NetworkUtil networkUtil; | |
LoginApiClient loginApiClient; | |
setUp(() { | |
networkUtil = MockNetworkUtil(); | |
loginApiClient = LoginApiClient(networkUtil: networkUtil); | |
}); | |
test("return DataResponse when networkUtil calls succeed", () async { | |
var utilisateur = Utilisateur(login: "test", password: "test"); | |
var dataReq = DataRequest<Utilisateur>(data: utilisateur); | |
final expected = DataResponse<Utilisateur>(); | |
when(networkUtil.post(url: "/user/login", data: dataReq)) | |
.thenAnswer((_) async => expected); | |
var result = await loginApiClient.login(utilisateur); | |
expect(result, expected); | |
}); | |
}); | |
} | |
// login_api_client.dart | |
class LoginApiClient { | |
final NetworkUtil<Utilisateur> networkUtil; | |
LoginApiClient({this.networkUtil}); | |
Future<DataResponse<Utilisateur>> login(Utilisateur utilisateur) async { | |
DataRequest<Utilisateur> dataRequest = DataRequest<Utilisateur>(data: utilisateur); | |
var response = await networkUtil.post(url: "/user/login", data: dataRequest); | |
print("response = $response"); | |
return response; | |
} | |
} | |
//network_util.dart | |
class NetworkUtil<T> { | |
static final _BASE_URL = "api_url"; | |
final http.Client httpClient; | |
NetworkUtil({this.httpClient}); | |
Future<DataResponse<T>> post({String url, String tenantID = "", dynamic data}) async { | |
final response = await httpClient.post(_BASE_URL + url, | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
"tenantID": tenantID | |
}, | |
body: data.toJson()); | |
if (response.statusCode != 200) { | |
throw Exception('error getting http response'); | |
} | |
return DataResponse<T>.fromJson(json.decode(response.body.toString())); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment