Angular / Testing jasmine using dewered promises

I am testing a controller using a combination of angular and jasmine, and I'm not quite sure about using deffered promises.

This is my spec code.

describe('Controller Tests', function(){
    var scope, searchAPI;

    beforeEach(function(){
        var mockSearchAPI = {};
        module('myApp', function($provide){
            $provide.value('searchAPI', mockSearchAPI);
        });
    });

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

    beforeEach('Main Search Controller Tests', function(){

        function($controller, $rootScope, _searchAPI_){
            scope = $rootScope.$new();
            searchAPI = _searchAPI_;
            $controller('MainSearchCtrl', function(){
                $scope: scope,
                searchAPI: searchAPI
            });
            scope.$digest();
        }
    });

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

        //THIS LINE IS GIVING ME '{}'
        alert(promise); 
    });

});

I am not sure why the line I am "warning" gives me the output "{}". Isn't that a data structure that I defined as "testData" in the input function? I'm not sure what is going on here. I have tried many solutions.

My controller code is basically a wrapper for a service

$scope.processQueryByField = function(field, value, pageNumber){
    return searchAPI.executeSearch(field, value, pageNumber);
}

Should I just get the value that I defined inside the injector code?

+4
source share
1 answer

, . , , , , .

: -

, : -

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

, : -

.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;
          })
        }
    }]);

# 1: - , api .

   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); 
    });

# 2: - , , .

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");
});

+3

All Articles