How to ensure that jasmine expect () is executed in a test

I have a test like this for an angular application:

it("should return false if all products loaded", function () { $httpBackend.flush(); scope.loadNextProducts(15).then(function (isThereMoreToLoad) { expect(isThereMoreToLoad).toBe(false); }); scope.$apply(); }); 

If I forgot to write the broadcast $httpBackend.flush(); or scope.$apply(); , then the test will never reach the expect() , and the test will succeed.

Is there a way to ensure that the expect () jasmine test runs, and if not, should it fail?

Something like telling it() how much expect() expect, or tell jasmine that every test must run at least one expect() , otherwise it should fail.

+4
source share
1 answer

Try the following:

  afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); 

This should ensure that your tests do not have expectations that have not been visited, and should also ensure that all expected server calls have been called.

Further information on Angular $ backend docs can be found.

0
source

All Articles