簡體   English   中英

TypeError:'undefined'不是函數(評估'mockBackend.expectPost(

[英]TypeError: 'undefined' is not a function (evaluating 'mockBackend.expectPost(

我試圖用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)

我不明白為什么我得到這個錯誤,因為我在我的業力conf中正確包含了angular-mock.js文件:

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

改變第16行

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

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

不知何故解決了這個問題......

編輯expectPOST也會有效...只是一個錯字:注意我使用的情況......

可能你的情況類似於:

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

ab未定義,試圖調用它會給你錯誤。

這里的第16行是$scope.saveCurriculum() ,是$scope.saveCurriculum undefined?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM