Angular/Jasmine testing with deffered promises

和自甴很熟 提交于 2019-12-05 13:22:27

I am not exactly sure what expectation you want to set with the promise object, You do not need to test if a promise is resolved, instead you need to test what happens to the data when a promise is resolved.

Example:-

Change your mock to simplify:-

inject(function($q){
    var testData = {"message":"hi"};
    mockSearchAPI.executeSearch = function(){
       return $q.when(testData);
    };
});

Just for a demo i have added one more method in your controller which resolved to a data:-

.controller('MainSearchCtrl', ['$scope','searchAPI', function ($scope, searchAPI) {
        //At this point placing this method on scope is useless
        $scope.processQueryByField = function(field, value, pageNumber){
           return searchAPI.executeSearch(field, value, pageNumber);
        }
        //This when invoked will update the searchResults in the model.
        $scope.populateData = function(){
          $scope.processQueryByField(1,1,1).then(function(data){
            $scope.searchResults = data;
          })
        }
    }]);

Expectation #1:- Test whether when the method is invoked the api method is getting invoked with the expected arguments.

   it('should invoke execute search', function(){
     //Set up a spy on your mock api method
      spyOn(mockSearchAPI,'executeSearch'); 
      var field = "testfield";
      var value = "testvalue";
      var pageNumber = 1;
      scope.processQueryByField(field, value, pageNumber); //invoke scope method with args
      //Test if the mock api method has been called
      expect(mockSearchAPI.executeSearch).toHaveBeenCalled();          
      //test if it has been called with expected arguments.
      expect(mockSearchAPI.executeSearch).toHaveBeenCalledWith(field, value, pageNumber); 
    });

Expectation #2:- Test whether data is populated properly when the promise is resolved.

it('should return a promise correctly', function(){
    var field = "testfield";
    var value = "testvalue";
    var pageNumber = 1;

    var promise = scope.processQueryByField(field, value, pageNumber);
    //This is a useless expectation
    expect(promise).toBeDefined();


    scope.populateData();
    $rootScope.$digest(); //<-- Apply digest cycle, so the promise is resolved
    expect(scope.searchResults).toBeDefined();
    expect(scope.searchResults.message).toEqual("hi");
});

Test Demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!