POST请求中的测试和存根参数

时间:2016-08-07 11:34:26

标签: javascript node.js unit-testing mocha sinon

伙计们如何在POST请求中存根params,例如这里是函数的一部分

  it('grid Calculate', function (done) {
    var req = {
     'DateFirstLicensed' : "01-01-2010",
   'DateFirstInsured': "01-01-2011",
   'ClaimList': ['05-03-2012'],
   'SuspenList': [{'StartDate':'05-03-2012','EndDate':'05-05-2012' }]
    };
    gridCalculator.gridCalculator(req,function (err, result) {
      result.should.exist;
      done();
    });
  });

如果我这样做

 TypeError: req.param is not a function

我收到错误,因为我只是传递一个对象而不是POST请求

<div ng-controller="AppCtrl" ng-app="myApp">
        <div ng-init="showManu()"></div>  
      <div id="menu"></div>
</div>
       var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope','$http','$compile',function ($scope, $http, $compile) {
    $scope.roots = [{
    Folder_Name:'root1',
    _id:'1',
    parent:null
    },{
    Folder_Name:'root2',
    _id:'2',
    parent:null
    },{
    Folder_Name:'newrow',
    _id:'9',
    parent:'1'
    },{
    Folder_Name:'chirag',
    _id:'3',
    parent:'1'
    },{
    Folder_Name:'sumit',
    _id:'4',
    parent:'2'
    },{
    Folder_Name:'vikas',
    _id:'5',
    parent:'4'
    }
    ];


        $scope.showManu = function(){
                    angular.forEach($scope.roots, function(root){
            if(root.parent == null){
                        var myEl = angular.element(document.getElementById('menu'));
        var html = '<ul><li id="'+root._id+'" ><a ng-click="hideShow('+root._id+')">'+root.Folder_Name+'</a></li></ul>';
        var element = $compile(html)($scope);
        myEl.append(element);
            }
          })
          var hideList = [];
          $scope.hideShow = function(id){
                    if(hideList.indexOf(id)==-1){
                            getLi(id);
                    hideList.push(id);
                }
                else{
                 var classHide = document.getElementById(id);
                 var x = classHide.getElementsByTagName("ul")
                console.log(x.length);
                for(var i = x.length; i>=1; i--){
                console.log(i);
                        classHide.removeChild(classHide.childNodes[i]);
                    }
                            hideList.splice(hideList.indexOf(id),1);


                }
            }

          function getLi(root){
            angular.forEach($scope.roots, function(r){
                    if(r.parent == root){
                        var myEl = angular.element(document.getElementById(r.parent));
        var html = '<ul class="'+r.parent+'"><li id="'+r._id+'"><a ng-click="hideShow('+r._id+')">'+r.Folder_Name+'</a></li></ul>';
        var element = $compile(html)($scope);
        myEl.append(element);
                }
            })
          }


            }



}]);

2 个答案:

答案 0 :(得分:1)

我想到了两个选项(可能还有更多):

选项1:自己定义param函数:

  it('grid Calculate', function (done) {
    var params = function(param) {
      switch (param) {
        case 'DateFirstLicensed':
          return "01-01-2010";
        case 'DateFirstInsured':
        ... //do the same for all params 
      }
    };
    var req = {
     param: params
    };
    gridCalculator.gridCalculator(req,function (err, result) {
      result.should.exist;
      done();
    });
  });

选项2:使用supertest之类的工具创建对服务器端点的调用。

答案 1 :(得分:0)

问题是你没有在测试中存储gridCalculator方法中使用的函数。

它应该是这样的:

it('grid Calculate', function (done) {
    var testParams = {
       'DateFirstLicensed' : "01-01-2010",
       'DateFirstInsured': "01-01-2011",
       'ClaimList': ['05-03-2012'],
       'SuspenList': [{'StartDate':'05-03-2012','EndDate':'05-05-2012'}]
    };
    var req = {
        param: function (paramName) {
            return testParams[paramName];
        }
    };
    gridCalculator.gridCalculator(req,function (err, result) {
        result.should.exist;
        done();
    });
});