Bonjour,
J'ai un service et j'essaye de mocker un appel http mais j'ai une erreur que je n'arrive pas à résoudre.
Voici mon code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| (function () {
"use strict";
angular.module("testmodule", [])
.factory("TestFactory", [ServiceTest])
function ServiceTest($http) {
var testData;
var ServiceManager = {
_pool: {},
retrieve: function (id, data) {
// faire quelque chose
},
load: function (callback) {
console.log("Fonction Load")
$http.get('/entries').success(function (data) {
testData = data;
callback();
});
}
,
getAll: function (hub_id) {
}
};
return ServiceManager;
}
}); |
Le test est le suivant :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
describe('****** Mock Test ******', function () {
var $http;
var serviceManagerFactory;
var $httpBackend;
beforeEach(function () {
// we load our module
module('testmodule');
inject(function ($injector) {
// We get our factory by is name
serviceManagerFactory = $injector.get('TestFactory');
$http = $injector.get('$http');
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('GET', '/entries').respond([
{id: 1, name: "Entry 1"},
{id: 2, name: "Entry 2"}
]);
});
});
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('My Service Test', function () {
var data = [{id: 1, name: 'Entry 1'}, {id: 2, name: 'Entry 2'}];
serviceManagerFactory.load(function () {
expect(testData.length).toBe(2);
expect(testData.entries).toEqual(data);
});
$httpBackend.flush();
});
}); |
L'erreur se trouve au niveau de la ligne $httpBackend.flush();.
Voici la sortie d'erreur :
Error: No pending request to flush !
Partager