Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Jasmine测试AngularJS控制器中是否存在属性?_Angularjs_Bdd_Jasmine - Fatal编程技术网

如何使用Jasmine测试AngularJS控制器中是否存在属性?

如何使用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(

我有一个controllers.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");

    }));

  });

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