I'm encountering some issues passing certain tests after implementing $httpBackend.verifyNoOustandingRequest().
Interestingly, excluding this from my afterEach function allows the tests to pass successfully. However, including it causes all tests to fail except for the last one 'should load items from the server'. Each failed test shows the response as 'Error: unflushed request: 1' except for the final test.
It's puzzling because I was under the impression that flushing is only necessary when mocking an Ajax request, yet it's causing failures in all tests except the actual server test.
Any assistance on this matter would be greatly appreciated.
describe('controller: ListCtrl', function(){
beforeEach(module('notesApp'));
var ctrl, loc, mockService, itemServiceS, mockBackend;
beforeEach(inject(function($controller, $location, ItemService, $httpBackend){
spyOn(ItemService, 'list').and.returnValue([{id: 1, label: 'First', done: true}]);
itemService = ItemService;
mockBackend = $httpBackend;
mockBackend.expectGET('/api/note').respond([{id: 1, label: 'Mock'}]);
ctrl = $controller('ListCtrl');
loc = $location;
}));
it('Should have items available on load', function(){
expect(ctrl.items).toEqual([
{id: 1, label: 'First', done: true},
{id: 2, label: 'Second', done: false}
]);
});
it('Should have highlight items based on state', function(){
var item = {id: 1, label: 'First', done: true};
var actualClass = ctrl.getDoneClass(item);
expect(actualClass.finished).toBeTruthy();
expect(actualClass.unfinished).toBeFalsy();
item.done = false;
actualClass = ctrl.getDoneClass(item);
expect(actualClass.finished).toBeFalsy();
expect(actualClass.unfinished).toBeTruthy()
});
it('should change the url', function(){
expect(loc.path()).toEqual('');
ctrl.navigate1();
expect(loc.path()).toEqual('/some/where/else');
});
it('Should change the url to /some/where', function(){
expect(loc.path()).toEqual('');
ctrl.navigate2();
expect(loc.path()).toEqual('/some/where');
});
it('Should have called through ItemService factory', function(){
expect(itemService.list).toHaveBeenCalled();
expect(itemService.list.calls.count()).toEqual(1);
expect(ctrl.itemsGet).toEqual([{id: 1, label: 'First', done: true}]);
});
it('Should load items from server', function(){
expect(ctrl.retrievedItems).toEqual([]);
mockBackend.flush();
expect(ctrl.retrievedItems.data).toEqual([{id: 1, label: 'Mock'}]);
});
afterEach(function(){
mockBackend.verifyNoOutstandingExpectation();
mockBackend.verifyNoOutstandingRequest();
})
});