加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用默认承诺进行角度/茉莉花测试

发布时间:2020-12-17 17:13:48 所属栏目:安全 来源:网络整理
导读:我正在使用角度和茉莉花的组合来测试控制器,并且我不完全确定使用默认的承诺. 这是我的规范代码. describe('Controller Tests',function(){ var scope,searchAPI; beforeEach(function(){ var mockSearchAPI = {}; module('myApp',function($provide){ $prov
我正在使用角度和茉莉花的组合来测试控制器,并且我不完全确定使用默认的承诺.

这是我的规范代码.

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,pageNumber){
    return searchAPI.executeSearch(field,pageNumber);
}

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

解决方法

我不确定您希望使用promise对象设置什么期望,您不需要测试是否已解决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,pageNumber){
           return searchAPI.executeSearch(field,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,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,pageNumber); 
    });

期望#2: – 在解决承诺时测试数据是否正确填充.

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

    var promise = scope.processQueryByField(field,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读