角度/茉莉花测试与延期承诺

时间:2014-09-22 21:33:57

标签: angularjs unit-testing jasmine deferred angular-promise

我正在使用角度和茉莉花的组合来测试控制器,并且我不完全确定使用默认的承诺。

这是我的规范代码。

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

});

我不确定为什么我“警告”的行给我输出'{}'。它不应该是我在注入函数中定义为“testData”的数据结构吗?我不确定这里发生了什么。我尝试了很多解决方案。

我的控制器代码基本上是服务的包装器

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

我不应该只接收我在进样器代码中定义的值吗?

1 个答案:

答案 0 :(得分:3)

我不确定你想用promise对象设置什么期望,你不需要测试一个promise是否已经解决,而是你需要测试承诺得到解决。

实施例: -

将模拟更改为简化: -

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

<强> Test Demo