javascript - 类型错误 : 'undefined' is not a function (evaluating 'mockBackend. expectPost(

标签 javascript angularjs karma-runner

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

这是我的测试套件:

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

    });
});

这是 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)

我不明白为什么会出现此错误,因为我已将 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'      
],

有人可以帮忙吗?

编辑:这是我的 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("/");
        });
    };
}

最佳答案

改变第 16 行

来自

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

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

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

编辑:expectPOST 也可以工作...只是一个打字错误:注意我使用的大小写...

关于javascript - 类型错误 : 'undefined' is not a function (evaluating 'mockBackend. expectPost(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588153/

相关文章:

javascript - 响应式设计 : switching order of Div's

javascript - 未捕获的类型错误 : Illegal invocation on addEventListener

javascript - fs.stat和fs.readFileSync之间的Node.js错误处理相关性

javascript - Angular Material - 如何开始?

javascript - $scope 中的值在 location.path 之后不可见

javascript - `java` 是 JavaScript 中的保留关键字吗?

php - 使用 AngularJS 显示数据库中的数据

intellij-idea - 从我的 IDE 调试 angular2/karma 测试时出现问题 : breakpoints are never hit

karma-runner - 如何在 Visual Studio 中调试 typescript jasmine 测试?

angularjs - 为 Hybrid Angular 2/1x 应用设置 Karma 配置