mercredi 6 juin 2018

How can we do unit testing asynchronous functions using test package in angulardart?

I tried creating async function using angulardart code which will call internal service and from service will return response from a test url. How will i mock the particular function getUserDetails().

login_component.html

<div class="container">
<button type="button" (click)="getUserDetails()">Click me to see 
details</button>
</div>
<div *ngFor="let user of userDetails"></div>

login_component.dart:

Future<Null> getUserDetails() async {
try {
userDetails = await _loginService.userDetails();
print(userDetails);
} catch (e) {
errMsg = e.toString();
}}

login_service.dart:

Future<dynamic> userDetails() async {
var _url = 'https://jsonplaceholder.typicode.com/users'; // URL to JSON file
try {
var res = await _http.get(_url);
var response = _extractData(res).map((value) => new 
List.fromJson(value)).toList();
return response;
} catch (e) {
print(e);
}
}
dynamic _extractData(Response resp) => JSON.decode(resp.body);

list.dart

class List {
int id;
String name;
List(this.id, this.name);
factory List.fromJson(Map<dynamic, dynamic> list) =>
new List(list['id'], list['name']);
Map toJson() => {'id': id, 'name': name};
}

Please help me in finding out solution. Also how can i check the test code coverage in angulardart ?

Aucun commentaire:

Enregistrer un commentaire