Angular.js promise not resolving when unit testing service with karma

前端 未结 4 1292
梦如初夏
梦如初夏 2021-01-18 19:03

I am trying to unit test an Angular.js service, and need to set an expect on a promise returned from a Mock service (using Jasmine). I am using the karma unit testing framew

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 19:34

    Assuming that you are using ngFacebook/ngModule a quick note before the solution/ideas is that this project does not have unit tests ! Are you sure you want to use this project ?

    I did a quick scan of your Unit Tests on Github and found following missing:-

    1) Module initialization. ngFacebook needs that or you need to initialize your module that does the same thing.

    beforeEach(module('ngFacebook'));
    

    OR

    beforeEach(module('yieldtome'));
    

    2) Seriously consider mocking ngFacebook module

    At unit level tests you are testing your code within a mocked bubble where outside interfaces are stubbed out.

    Otherwise) Try adding calling the API as below:-

     $rootScope.$apply(function() {
         this.FacebookService.getFacebookToken().then(function(){
            //your expect code here
         });
        });
     $httpBackend.flush();//mock any anticipated outgoing requests as per [$httpBackend][2]
    

提交回复
热议问题