Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jasmine .then不是一个函数Angularjs工厂_Jasmine - Fatal编程技术网

Jasmine .then不是一个函数Angularjs工厂

Jasmine .then不是一个函数Angularjs工厂,jasmine,Jasmine,我刚开始学习angularjs的Jasmine测试用例。我无法测试下面的代码。请帮助 $scope.getConstants = function(lovName) { ConstantService.getConstants(lovName).then(function(d) { switch (lovName) { case 'WORKFLOW': $scope.workflowTypes = d; $s

我刚开始学习angularjs的Jasmine测试用例。我无法测试下面的代码。请帮助

$scope.getConstants = function(lovName) {
    ConstantService.getConstants(lovName).then(function(d) {
        switch (lovName) {
        case 'WORKFLOW':
            $scope.workflowTypes = d;
            $scope.loadCounterpartyTmp();
            break;
            --------Other Cases
            }
我的ConstantService定义为

 App.factory('ConstantService', [ '$http', '$q', function($http, $q) {

return {
    getConstants : function(lovName) {
        return $http.post('/sdwt/data/getConstants/', lovName).then(function(response) {
            return response.data;
        }, function(errResponse) {
            return $q.reject(errResponse);
        });
    }
我想测试getConstants函数。我需要创建一个ConstantService的模拟,并将数据传递给它

我已经写了下面的测试用例,但是测试用例不起作用。请让我知道如何测试上面的代码

  describe('getConstantsForMurexEntity', function() {
            it('testing getConstantsForMurexEntity function', function() {

   var d=[];
                d.push(
                    {id:1,value:'ABC'},
                    {id:2,value:'DEF'},
                    {id:3,value:'IJK'},
                    {id:4,value:'XYZ'},
                       );
                   //defined controller
                    spyOn(ConstantService, 'getConstants').and.returnValue(d);

                $scope.getConstants('WORKFLOW');
                expect($scope.workflowTypes).toBe(d);

上面的测试用例不起作用,因为它说“ConstantService.getConstants(…)。then不是一个函数”。

您的
ConstantService.getConstants()
函数通过
.then()
调用返回一个承诺,您的实际代码正在使用该承诺。这意味着,当你监视它时,你还需要回报一个承诺,而你并没有这样做。因为您没有返回承诺,所以当实际调用尝试调用
.then()
时,它是未定义的,这就是错误消息的原因

另外,您没有使用
数组。请正确按下

您的测试可能应该如下所示(注意,这是未测试的):


嗨,谢谢你的回复。我在尝试运行测试用例时使用了上述代码,它表示$scope.workflowTypes未定义。你能告诉我为什么它是未定义的吗?没有看到你的代码,我恐怕猜不出为什么。我会尝试通过调试或在代码中添加console.logs来将其缩小到哪一行。我在这里添加了一个类似的答案:
describe('getConstantsForMurexEntity', function() {
  it('should set workflowTypes to the resolved value when lovName is "WORKFLOW"', inject(function($q) {
    var deferred = $q.defer();
    spyOn(ConstantService, 'getConstants').and.returnValue(deferred.promise);

    var d = [
      {id:1,value:'ABC'},
      {id:2,value:'DEF'},
      {id:3,value:'IJK'},
      {id:4,value:'XYZ'},
    ];

    $scope.getConstants('WORKFLOW');

    deferred.resolve(d);
    $scope.$apply();

    expect($scope.workflowTypes).toBe(d);
  }));
});