javascript - Error: No pending request to flush - when unit testing AngularJs service -
i'm newbie angularjs, , i'm in process of writing first unit test; test service wrote test returns single json object. however, everytime run test error stated in title. don't know causing this! tried reading on $apply
, $digest
, not sure if that's needed in case, , if yes how; simple plunker demo appreciated.
here code
service
var allbookss = []; var filteredbooks = []; /*here define our book model , rest api.*/ var report = $resource('api/books/:id', { id: '@id' }, { query: { method: 'get', isarray: false } }); /*retrive requested book internal book list.*/ var getbook = function(bookid) { var deferred = $q.defer(); if (bookid === undefined) { deferred.reject('error'); } else { var books= $filter('filter')(allbooks, function(book) { return (book.id == bookid); }); if (books.length > 0) { deferred.resolve(books[0]);//returns single book object } else { deferred.reject('error'); }; }; return deferred.promise; };
test
describe('unit:bookservice', function(){ beforeeach(module('myapp')); var service, $httpbackend; beforeeach(inject(function (_bookservice_, _$httpbackend_) { service = _bookservice_; $httpbackend = _$httpbackend_; $httpbackend.when('get', "/api/books/1").respond(200, { "book": { "id": "1", "author": "james spencer", "edition": "2", ..... } }); })); aftereach(function() { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); it('should return metadata single report', function() { service.getbook('1').then(function(response) { expect(response.length).toequal(1); }); $httpbackend.flush();// error in line }); });
error
error: no pending request flush ! @ c:/myapp/bower_components/angular-mocks/angular-mocks.js:1439 @ c:/myapptest/tests/booktest.js:34
libs version
angularjs v1.2.21
angularjs-mock v1.2.21
i don't see you're issuing report.query(). getbook function returns unresolved promise never resolved because nothing in function async.
you need call report.query via book function promise resolved in .then() (in book function). after that, flush http backend in service.getbook().then() , expect.
Comments
Post a Comment