javascript - Jasmine testing with $httpBackend for Angularjs controller -


i made angular project — simple music library application. allows display, edit or delete data albums. communication server made via api. now, project works pretty good, need write unit tests. problem is, can't find clear tutorial on how in case.

let me provide code.

albums.controller.js:

'use strict';  angular.module('albums').controller('albumscontroller', ['albumservice', '$location', function (albumservice, $location) {     var vm = this;      albumservice.getdata().then(function successcallback(data) {         vm.albums = data;     }, function errorcallback(data) {         console.log('error!');         console.log(data);     }); }]); 

albumservice.service.js:

'use strict';  angular.module('core').service('albumservice', function ($http) {      this.getdata = function () {         var promise = $http.get('/albums/list')             .then(function successcallback(r) {                 return r.data;             }, function errorcallback(r) {                 return r.data;             });         return promise;     } }); 

albums.component.spec.js:

'use strict';  describe('albums', function () {      beforeeach(module('albums'));      describe('albumscontroller', function () {         var $httpbackend, ctrl, serverresponse;          serverresponse = {             "0": {                 "title": "cool album",                 "artist": "unknown",                 "country": "usa",                 "company": "umg",                 "price": 7.9,                 "year": 2005,                 "id": 0             },             "1": {                 "title": "bright vibes",                 "artist": "jonnie rush",                 "country": "uk",                 "company": "sunny records",                 "price": 8.99,                 "year": 2011,                 "id": 1             }         };          beforeeach(inject(function ($componentcontroller, _$httpbackend_) {             $httpbackend = _$httpbackend_;             $httpbackend.expectget('/albums/list')                 .respond(serverresponse);              ctrl = $componentcontroller('albums');         }));          it('should create `albums` property 2 albums fetched `$http`', function () {             jasmine.addcustomequalitytester(angular.equals);              expect(ctrl.albums).toequal([]);              $httpbackend.flush();             expect(ctrl.albums).toequal(serverresponse);         });     });  }); 

but karma output next:

expected undefined equal [  ].         src/tests/albums.component.spec.js:42:40         loaded@http://localhost:9999/context.js:162:17         error: no pending request flush ! in src/html/bower_components/angular-mocks/angular-mocks.js (line 1846)         flush@src/html/bower_components/angular-mocks/angular-mocks.js:1846:83         src/tests/albums.component.spec.js:44:31         loaded@http://localhost:9999/context.js:162:17 phantomjs 2.1.1 (linux 0.0.0): executed 1 of 1 (1 failed) error (0.047 secs / 0.017 secs) 

i tried figure out problem myself, spent whole day it. frankly talking i'm newbie testing, remains unclear me how make work. great, if me.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -