我试图单元测试与因果报应,和茉莉花的angularjs控制器。
下面是我的测试套件:
描述('控制器',函数(){ 变量$范围,CTRL; beforeEach(模块('curriculumModule')); 描述(CreateCurriculumCtrl',函数(){ VAR mockBackend,位置; beforeEach(注(函数($ rootScope,$控制器_ $ httpBackend_,$位置){ mockBackend = _ $ httpBackend_; 位置= $位置; $范围= $ rootScope $新的()。 CTRL = $控制器('CreateCurriculumCtrl',{ $范围:$范围 }); })); 它('要救课程',函数(){ mockBackend.expectPost('bignibou /课程/新); $ scope.saveCurriculum(); mockBackend.flush(); 期待(location.path())toEqual(/)。 }); });});
下面是因果报应的开始的输出:
PhantomJS 1.9.2(Linux)的控制器CreateCurriculumCtrl应该拯救课程失败 类型错误:未定义不是一个函数(计算'mockBackend.expectPost('bignibou /课程/新)) 在/home/julien/Documents/projects/site-garde-enfants/bignibou/karma/test/spec/curriculum.test.js:16PhantomJS 1.9.2(Linux)的:执行的2(1失败)(0.203秒/ 0.023秒)
我不明白为什么我得到这个错误,因为我已经正确地包含在我的人缘conf下角mock.js文件:
//的文件/目录模式在浏览器中加载文件: ../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', 测试/供应商/角mocks.js', ../src/main/webapp/js/custom/curriculum.js', 测试/规格/ curriculum.test.js]
任何人都可以请帮助?
修改:这里是我的控制器:
函数CreateCurriculumCtrl($范围,$ HTTP,$位置,select2Options){ $ scope.formData = {}; $ scope.select2Options = select2Options; $ scope.saveCurriculum =功能(){ $ http.post('bignibou /课程/新',$ scope.formData).success(功能(数据){ 如果(data.status ==OK){} 如果(data.status ==KO){} $ location.path(/); }); };}
解决方案
更改行16
从
mockBackend.expectPost('bignibou /课程/新);
到
mockBackend.expect('POST','bignibou /课程/新)响应({})。
在某种程度上解决了该问题...
修改: expectPOST
将工作太...只是一个错字:注意我用的情况下...
I am trying to unit test an angularjs controller with Karma, and jasmine.
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:
// 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
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...