您好我正在使用Jasmine编写我的第一个角度测试,但我一直收到错误 这是我的控制器
(function () {
'use strict';
var dependencies = [];
angular.module('entityEdit', dependencies)
.config(configFn)
.run(runFn)
.directive('entityEdit', ['BASE_PATH', entityEditDirective])
.controller('EntityEditCtrl', ['$scope', '$rootScope','Entity', EntityEditCtrl])
function EntityEditCtrl($scope, $rootScope,Entity) {
$scope.entity = {};
$scope.list=[
{'id':"1",'libelle':'A' },
{'id':"2",'libelle':'B' },
]
$rootScope.$on('Entity_LIST_SELECTED', function (event, data) {
console.log("received");
$scope.entity = data;
});
$scope.save= saveFn;
function saveFn()
{
console.log("savefn");
console.log($scope.entity);
Entity.updateEntity($scope.entity);
}
}
function runFn() {
console.log('Run : entityEdit');
}
function configFn() {
console.log('Config : entityEdit');
}
})();
这是我的茉莉花测试
describe('EntityEditCtrl', function () {
var $rootScope, scope, $controller;
beforeEach(angular.mock.module('entityEdit'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
$controller('EntityEditCtrl', {
$scope: scope
});
ctrl = $controller('EntityEditCtrl',function(){});
}));
it('exists',inject(function($controller){
expect(ctrl).toBeDefined();
expect(ctrl).not.toBeNull();
expect(typeof ctrl).toBe('object');
}));
});
如果您发现问题,请告诉我
答案 0 :(得分:1)
单元测试,如果你想测试一个特定的单元,你需要注入该单元内所需的每个模块,而这里缺少的模块就是实体。
describe('EntityEditCtrl', function () {
var $rootScope, scope, controller,Entity;
beforeEach(angular.mock.module(core.entity'));
beforeEach(angular.mock.module('entityEdit'));
beforeEach(inject(function ($rootScope, $controller,_Entity_) {
scope = $rootScope.$new();
Entity=_Entity_;
controller=$controller('EntityEditCtrl', {$scope: scope});
}));
it('exists',inject(function($controller){
expect(ctrl).toBeDefined();
expect(ctrl).not.toBeNull();
expect(typeof ctrl).toBe('object');
}));
});