【发布时间】:2015-02-26 03:55:18
【问题描述】:
angular.js 的新手,不知道如何为具有 templateUrl 和隔离范围的指令编写测试。
这是我的控制器
(function(){
angular.module('buttons')
.controller('buttonController', ['$scope', function($scope){
//primary button
$scope.primaryButton = { name: 'Submit'};
})();
这是我的观点 index.html &
<div class="layoutLeft">
<p>Primary Button</p>
<primary-button info="primaryButton"></primary-button>
</div>
primary-button.html
<button class="{{buttonInfo.class}}">{{buttonInfo.name}}</button>
这是我的指令
(function(){
angular.module('buttons')
.directive('primaryButton', function() {
return {
restrict: 'EA',
scope: {
buttonInfo: '=info'
},
templateUrl: 'scripts/local/views/primary-button.html'
}
})
})();
这是我的测试
(function(){
beforeEach(angular.mock.module('buttons'));
describe('Buttons Directive Test', function(){
var $compile, $scope, $httpBackend;
beforeEach(module('templates'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$scope = _$rootScope_.$new();
$scope.primaryButton = {name: 'Save'};
elm = angular.element("<primary-button info='buttonInfo'></primary-button>");
e = $compile(elm)($scope);
e.digest();
}));
it('should do something', function(){
expect(e.html()).toContain($scope.primaryButton);
});
});
})();
我正在使用 jasmine 和 karma 进行测试,有人可以指导我做错了什么
【问题讨论】:
-
我的解决方案是否解决了您的问题?请告诉我。
标签: javascript angularjs jasmine karma-jasmine