【发布时间】:2015-09-13 09:22:27
【问题描述】:
我试图在我的 jasmine 测试中通过 $scope 检索控制器,但失败得很惨。有人知道为什么吗?
当使用controllerAs 语法时,控制器对象使用controllerAs 中指定的名称放在$scope 对象上。因此,通过使用 ng-app='MyApp' 在浏览器中运行下面的代码来引导到 angular,我可以使用 chrome-dev 工具来定位和选择指令元素,然后在控制台中键入 $0.scope().myDirCtrl。这确实产生了控制器对象,那么为什么我不能在我的单元测试中检索控制器对象呢?
运行下面的 sn-p 将启动一个独立的 jasmine 浏览器测试环境。测试规范列在 sn-p 的底部。我遇到问题的代码是这样的:
expect($scope.myDirCtrl).toBeDefined();
/* --------------------------------------
Source code
--------------------------------------*/
(function(angular) {
'use strict';
// Setup the template -----------------
angular.module('MyApp.tpls', [])
.run(['$templateCache', function($templateCache) {
$templateCache.put('partials/myDirective.html',
'<div>{{myDirCtrl.testValue}}</div>');
}]);
// Setup the app ----------------------
angular.module('MyApp', ['MyApp.tpls'])
.directive('myDirective', myDirective)
.controller('MyDirectiveController', MyDirectiveController);
function myDirective() {
return {
restrict : 'E',
templateUrl : 'partials/myDirective.html',
transclude : true,
controllerAs : 'myDirCtrl',
bindToController: true,
scope : {},
controller : 'MyDirectiveController'
};
}
MyDirectiveController.$inject = ['$scope'];
function MyDirectiveController($scope) {
var ctrl = this;
ctrl.testValue = 'Only a test';
}
})(angular);
/* --------------------------------------
Test specifications
--------------------------------------*/
(function (module) {
'use strict';
// Define the tests -------------------
describe('My directive test', function () {
var $compile, $rootScope, $scope;
beforeEach(module('MyApp'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
}));
it('scope should contain a controller reference', function () {
var element = $compile(angular.element('<my-directive></my-directive>'))($scope);
$scope.$digest();
expect($scope.myDirCtrl).toBeDefined();
});
});
})(module);
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-mocks.js"></script>
【问题讨论】:
标签: javascript angularjs unit-testing jasmine