如何使用Jasmine测试AngularJS控制器中是否存在属性?
我有一个controllers.js文件,如下所示:如何使用Jasmine测试AngularJS控制器中是否存在属性?,angularjs,bdd,jasmine,Angularjs,Bdd,Jasmine,我有一个controllers.js文件,如下所示: angular.module('MyApp.controllers', []). controller('MyCtrl', [function() { $scope.type = "default"; }]); describe('controllers', function(){ beforeEach(module('MyApp.controllers')); describe('MyCtrl', function(
angular.module('MyApp.controllers', []).
controller('MyCtrl', [function() {
$scope.type = "default";
}]);
describe('controllers', function(){
beforeEach(module('MyApp.controllers'));
describe('MyCtrl', function() {
it('should have a property named "type" whose default value is "default"', inject(function() {
expect(MyCtrl.type).toBe("default");
}));
});
});
controllersSpec.js如下所示:
angular.module('MyApp.controllers', []).
controller('MyCtrl', [function() {
$scope.type = "default";
}]);
describe('controllers', function(){
beforeEach(module('MyApp.controllers'));
describe('MyCtrl', function() {
it('should have a property named "type" whose default value is "default"', inject(function() {
expect(MyCtrl.type).toBe("default");
}));
});
});
如何测试MyCtrl
控制器是否具有类型
属性,并且该属性的默认值是“default”
字符串
此外,这种类型的测试值得吗?或者我应该重写它吗?如果我应该重写它,那么如何重写?因为您正在测试
$scope
的属性,而不是控制器函数,所以需要使用模拟的$scope
模拟整个Ctrl创建过程
var scope, controller;
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
controller = $controller('MyCtrl', {$scope: scope});
}));
it('should have a default type when created', function() {
expect(scope.type).toBe("Default")
});