AngularJS unit test for directive that queries server returns "Unsatisfied requests" -
i have bookmarking app takes in url , automatically extracts summary. when client requests server add new bookmark, server sends initial information , initiates process extract summary.
in angular frontend, have created directives adding bookmark , managing each item in bookmarks list. within listitem directive there checksummary()
method poll server summary.
i having problems unit test latter directive. fails "unsatisfied request" when log console @ various points $http.get()
request seems fire , can see $scope variables updated don't understand why fail cause. i've checked answers many different question can't find give insight.
the code following:
bookmarks.js:
angular.module( 'userpages.bookmarks', [ 'ui.router', 'ui.bootstrap', 'ui.validate' ]) .config(function config( $stateprovider ) { $stateprovider.state( 'userpages.bookmarks', { url: '/bookmarks', templateurl: 'userpages/bookmarks/bookmarks.tpl.html', controller: 'bookmarkscontroller', data: { pagetitle: 'bookmarks' }, }); }) .factory('bookmarksapiresource', ['$http', function ($http) { var bookmarksurl = '/api/v1/bookmarks/'; var api = { getlist: function() { return $http.get( bookmarksurl ) .then( function(response) { return response.data; }); }, getbyid: function(id) { return $http.get( bookmarksurl + id + '/' ) .then( function(response) { return response.data; }); }, addbookmark: function(articleurl) { return $http.post( bookmarksurl, {article_url: articleurl}) .then( function(response) { return response.data; }); }, checksummary: function(id) { return $http.get( bookmarksurl + id + '/?fields=summary' ) .then( function(response) { return response.data; }); } }; return api; }]) .controller( 'bookmarkscontroller', ['$scope', '$stateparams', 'bookmarksapiresource', function bookmarkscontroller( $scope, $stateparams, bookmarksapiresource) { $scope.username = $stateparams.username; $scope.templates = { bookmarks: { toolbar: 'userpages/bookmarks/bookmarks-toolbar.tpl.html', listitem: 'userpages/bookmarks/bookmarks-listitem.tpl.html', } }; $scope.addbookmarkformcollapsed = true; $scope.bookmarks = []; bookmarksapiresource.getlist().then( function(data) { $scope.bookmarks = data; }); }]) .directive('newbookmark', ['bookmarksapiresource', function(bookmarksapiresource) { var newbookmark = { restrict: 'e', templateurl: 'userpages/bookmarks/bookmarks-add-form.tpl.html', replace: true, link: function($scope, $element, $attrs, $controller) { $scope.addbookmark = function(articleurl) { var newbookmark = bookmarksapiresource.addbookmark(articleurl); $scope.bookmarks.push(newbookmark); }; } }; return newbookmark; }]) .directive('bookmarkslistitem', ['bookmarksapiresource', '$timeout', function(bookmarksapiresource, $timeout) { var listitem = { restrict: 'e', templateurl: 'userpages/bookmarks/bookmarks-listitem.tpl.html', replace: true, scope: true, link: function($scope, $element, $attrs, $controller) { var checksummary = function() { if (!$scope.bookmark.summary_extraction_done) { bookmarksapiresource.checksummary($scope.bookmark.id).then(function(data){ if (data.summary_extraction_done) { $scope.bookmark.summary_extraction_done = data.summary_extraction_done; $scope.bookmark.summary = data.summary; } else { $timeout(checksummary, 1000); } }); } checksummary(); } }; return listitem; }]) ;
and test follows:
bookmarks.spec.js
describe( 'userpages.bookmarks', function() { var $rootscope, $location, $compile, $controller, $httpbackend; var $scope, elem; beforeeach( module( 'userpages.bookmarks', 'ui.router' ) ); ... other tests ... describe('bookmarks-listitem', function() { var testbookmark; beforeeach(module('userpages/bookmarks/bookmarks-listitem.tpl.html')); beforeeach(inject(function(_$rootscope_, _$compile_, _$httpbackend_){ $rootscope = _$rootscope_; $compile = _$compile_; $httpbackend = _$httpbackend_; testbookmark = { id: 1, title: 'title of our first article', url: 'http://www.example.com/some/article', summary_extraction_done: false, summary: '', }; })); it('when summary_extraction_done = false, checksummary() should call server , update summary response', function () { $httpbackend.when('get', '/api/v1/bookmarks/1/?fields=summary').respond(200, { summary_extraction_done: true, summary: 'this summary article 1.' }); $httpbackend.expect('get', '/api/v1/bookmarks/1/?field=summary'); $scope = $rootscope.$new(); $scope.bookmark = testbookmark; elem = $compile('<bookmarks-listitem></bookmarks-listitem>')($scope); $scope.$digest(); $httpbackend.flush(); expect($scope.bookmark.summary_extraction_done).tobe(true); expect($scope.bookmark.summary).tobe('this summary article 1.'); }); }); });
and test results are:
firefox 27.0.0 (mac os x 10.9) userpages.bookmarks bookmarks-listitem when summary_extraction_done = false, checksummary() should call server , update summary respone failed error: unsatisfied requests: /api/v1/bookmarks/1/?field=summary in .../frontend/vendor/angular-mocks/angular-mocks.js (line 1486) createhttpbackendmock/$httpbackend.verifynooutstandingexpectation@.../frontend/vendor/angular-mocks/angular-mocks.js:1486 createhttpbackendmock/$httpbackend.flush@.../frontend/vendor/angular-mocks/angular-mocks.js:1464 @.../frontend/src/app/userpages/bookmarks/bookmarks.spec.js:6
any suggestions helpful.
typo here:
$httpbackend.expect('get', '/api/v1/bookmarks/1/?field=summary');
you missing s on end of field.
Comments
Post a Comment