lundi 16 février 2015

angularjs http service unit testing

I am trying to test a simple service for learning purposes..However; I can't figure out how it must be done:


service:



.factory('myService', function($http) {
var myService = {
async: function() {
var promise = $http.get('test.json').then(function (response)
{
return response.data;
});
return promise;
}
};
return myService;
});


controller:



myService.async().then(function(d) {
$scope.data = d;
$scope.e = $scope.data.txt;
});


test:



'use strict';
describe("myService", function(){

beforeEach(module("testingExpApp"));

var myService,
$httpBackend;

beforeEach(inject(function(myService, _$httpBackend_){
myService = myService;
$httpBackend = _$httpBackend_;
}));

describe("get", function(){

it('should return test.json data', function () {
var url = "../mock/test.json";
var x = $httpBackend.expectGET(url).respond(200, 'txt from json');

// flush response
$httpBackend.flush();
expect(x).toBe('txt from json');

});

});

});


I get 'no pending request to flush!'


I just want to test that myservice.get() get the test.json file data..I have tried everything but can't get it working..


Any tips?


Thanks a lot in advance!


Aucun commentaire:

Enregistrer un commentaire