I know that there are plenty of questions like this, but none of those worked for me and I don't really know what's going on and why they don't work on my specific case.
I'm trying to test a controller using $httpBackend.
Unfortunately, I always get
Unexpected request: GET default-system.json
No more request expected
The controller I'm trying to test loads data using the load() method of my factory:
angular.module('services', ['ngResource']).factory('myData', [
'$http', '$stateParams', function($http, $stateParams) {
var myData;
myData= function(Data) {
if (Data) {
return this.setData(Data);
}
};
myData.prototype = {
setData: function(Data) {
return angular.extend(this, Data);
},
load: function() {
var scope;
scope = this;
return $http.get('default-system.json').success(function(Data) {
return scope.setData(Data.data);
}).error(function(err) {
return console.error(err);
});
},
filtered: function() {
var scope;
scope = this;
return $http.get('default-system.json').success(function(Data) {
return angular.forEach(Data.data, function(item) {
var urlZoneId;
urlZoneId = parseInt($stateParams.zoneID);
if (item.iID === urlZoneId) {
return scope.setData(item);
}
});
});
}
};
return myData;
}
])
this is the controller I'm trying to test:
angular.module('menuController', ['services']).controller('menu', [
'$scope', 'myData', function($scope, myData) {
$scope.test= new myData();
$scope.test.load();
}
]);
and this is the unit test:
describe('Controller: menu', function() {
var $controller, $rootScope, myData, httpBackend, mockedRequest;
mockedRequest = {
"error": 0,
"ts": 1456151050,
"tm": "2016-02-22T14:24:10+0000",
"data": [{....},{....}]
]};
beforeEach(module('App'));
beforeEach(inject(function(_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
return $controller = _$controller_('zoneList', {
$scope: $rootScope
});
}));
beforeEach(inject((function($httpBackend, _myData_) {
myData = _myData_.prototype;
return httpBackend = $httpBackend;
})));
afterEach(function() {
httpBackend.flush();
httpBackend.verifyNoOutstandingExpectation(false);
return httpBackend.verifyNoOutstandingRequest();
});
return it('Should check that the controller is injected correctly', function() {
httpBackend.expectGET('default-system.json').respond(mockedRequest);
return myData.load().then(function(result) {
return console.log(result);
});
});
});
but I get the error mentioned above. Any ideas?
I'm really stuck and I can't figure out why it is not working.
Thanks in advance for any help
Aucun commentaire:
Enregistrer un commentaire