简体   繁体   中英

How to ensure that jasmine's expect() was executed in test

I have such test for angular app:

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 forget to write ether $httpBackend.flush(); or scope.$apply(); test will never reach expect() part and test will be successful.

Is there way to ensure that jasmine test executed expect(), and if not then it should fail?

Something like specifying to it() how many expect() to expect, or tell jasmine that each test should execute at least one expect() otherwise it should fail.

Try this:

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

That should, well, verify that your tests have no expectations that have not been visited and it should also verify that all of your expected server calls have been called.

You can find more info on the Angular $backend docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM