【问题标题】:TypeError: 'undefined' is not a function (evaluating 'mockBackend.expectPost(TypeError:“未定义”不是函数(评估“mockBackend.expectPost(
【发布时间】:2013-10-25 23:18:14
【问题描述】:

我正在尝试使用 Karma 和 jasmine 对 angularjs 控制器进行单元测试。

这是我的测试套件:

describe('Controllers', function(){
    var $scope, ctrl;
    beforeEach(module('curriculumModule'));
    describe('CreateCurriculumCtrl', function(){
        var mockBackend, location;
        beforeEach(inject(function($rootScope, $controller, _$httpBackend_, $location){
            mockBackend = _$httpBackend_;
            location = $location;
             $scope = $rootScope.$new();
             ctrl = $controller('CreateCurriculumCtrl', {
                    $scope: $scope
             });
        }));

        it('should save the curriculum', function(){
            mockBackend.expectPost('bignibou/curriculum/new');
            $scope.saveCurriculum();
            mockBackend.flush();
            expect(location.path()).toEqual("/");
        });

    });
});

这里是业力开始的输出:

PhantomJS 1.9.2 (Linux) Controllers CreateCurriculumCtrl should save the curriculum FAILED
    TypeError: 'undefined' is not a function (evaluating 'mockBackend.expectPost('bignibou/curriculum/new')')
        at /home/julien/Documents/projects/site-garde-enfants/bignibou/karma/test/spec/curriculum.test.js:16
PhantomJS 1.9.2 (Linux): Executed 2 of 2 (1 FAILED) (0.203 secs / 0.023 secs)

我不明白为什么会出现此错误,因为我已将 angular-mock.js 文件正确包含在我的 karma conf 中:

// list of files / patterns to load in the browser
files: [
   '../src/main/webapp/js/libs/jquery-1.10.2.js',
   '../src/main/webapp/js/libs/angular.js',
   '../src/main/webapp/js/libs/bootstrap.js',
   '../src/main/webapp/js/plugins/angularui.select2.js',
   'test/vendor/angular-mocks.js',

  '../src/main/webapp/js/custom/curriculum.js',
  'test/spec/curriculum.test.js'      
],

有人可以帮忙吗?

编辑:这是我的控制器:

function CreateCurriculumCtrl($scope, $http, $location, select2Options){

    $scope.formData={};

    $scope.select2Options = select2Options; 

    $scope.saveCurriculum = function(){
        $http.post('bignibou/curriculum/new', $scope.formData).success(function(data) {
            if(data.status=="OK"){}
            if(data.status=="KO"){}
            $location.path("/");
        });
    };
}

【问题讨论】:

    标签: javascript angularjs karma-runner


    【解决方案1】:

    更改第 16 行

    来自

    mockBackend.expectPost('bignibou/curriculum/new');
    

    mockBackend.expect('POST','bignibou/curriculum/new').respond({});
    

    以某种方式解决了这个问题...

    编辑expectPOST 也可以...只是一个错字:注意我使用的情况...

    【讨论】:

      【解决方案2】:

      您的情况可能类似于:

      var a = {};
      a.b();
      

      a.b 未定义,尝试调用它会报错。

      这里的第 16 行是$scope.saveCurriculum(),是$scope.saveCurriculum 未定义吗?

      【讨论】:

      • 其实我认为你是对的:mockBackend 似乎是一个函数,而不是一个我可以调用函数的对象...... mockBackend 的 console.log 给出了这个:function $httpBackend(method, url, data, callback, headers) { ... }
      • 第 16 行是这个:mockBackend.expectPost('bignibou/curriculum/new');
      猜你喜欢
      • 2018-08-04
      • 2014-01-01
      • 2015-03-30
      • 2023-04-03
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2014-09-13
      • 2018-08-27
      相关资源
      最近更新 更多