unit testing - angularjs - $httpbackend error: Unexpected request -
i know there plenty of questions this, none of worked me , don't know what's going on , why don't work on specific case.
i'm trying test controller using $httpbackend.
unfortunately,
unexpected request: default-system.json
no more request expected
the controller i'm trying test loads data using load() method of 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 controller i'm trying test:
angular.module('menucontroller', ['services']).controller('menu', [ '$scope', 'mydata', function($scope, mydata) { $scope.test= new mydata(); $scope.test.load(); } ]);
and 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_('menu', { $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 controller injected correctly', function() { httpbackend.expectget('default-system.json').respond(mockedrequest); return mydata.load().then(function(result) { return console.log(result); }); }); });
but error mentioned above. ideas?
i'm stuck , can't figure out why not working.
thanks in advance help
Comments
Post a Comment