简体   繁体   English

TypeError:'undefined'不是函数(评估'mockBackend.expectPost(

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

I am trying to unit test an angularjs controller with Karma, and jasmine. 我试图用Karma和jasmine对angularjs控制器进行单元测试。

Here is my test suite: 这是我的测试套件:

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

    });
});

Here is the output of karma start: 这是业力开始的输出:

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)

I don't understand why I get this error as I have correctly included the angular-mock.js file in my karma conf: 我不明白为什么我得到这个错误,因为我在我的业力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'      
],

Can anyone please help? 有人可以帮忙吗?

edit : here is my controller: 编辑 :这是我的控制器:

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

Changing line 16 改变第16行

from

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

to

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

somehow fixed the issue... 不知何故解决了这个问题......

edit : expectPOST would have worked too... Just a typo: notice the case I used... 编辑expectPOST也会有效...只是一个错字:注意我使用的情况......

Likely your situation will be similar to: 可能你的情况类似于:

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

ab is undefined, trying to call it will give you that error. ab未定义,试图调用它会给你错误。

Line 16 here is $scope.saveCurriculum() , is $scope.saveCurriculum undefined? 这里的第16行是$scope.saveCurriculum() ,是$scope.saveCurriculum undefined?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:undefined不是函数(评估调度) - TypeError: undefined is not a function (evaluating dispatch) TypeError:undefined'不是函数(评估'myAudio.play()')“ - TypeError: undefined' is not a function (evaluating 'myAudio.play()')" TypeError:undefined不是一个函数(评估'document.getsElementsByClassName('') - TypeError: undefined is not a function (evaluating 'document.getsElementsByClassName('') TypeError:'undefined'不是函数(评估'sinon.spy()') - TypeError: 'undefined' is not a function (evaluating 'sinon.spy()') TypeError:undefined不是一个函数(评估“ categories.map”) - TypeError: undefined is not a function (evaluating 'categories.map') TypeError:undefined不是函数(评估'$(“#typed”)。typed') - TypeError: undefined is not a function (evaluating '$(“#typed”).typed') TypeError:未定义不是函数(正在评估userItems.map) - TypeError: undefined is not a function (evaluating userItems.map) TypeError:未定义不是函数。 iOS Safari评估功能。 CoffeeScript - TypeError: Undefined is not a function. iOS Safari Evaluating function. CoffeeScript TypeError: undefined is not an object(评估“this”) - TypeError: undefined is not an object (evaluating 'this') 出现错误TypeError:'undefined'的DataTables不是函数(评估'$('#progresstable')。DataTable') - DataTables getting the error TypeError: 'undefined' is not a function (evaluating '$('#progresstable').DataTable')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM