我正在使用视频在一个角度的应用程序,我正在工作。我为它编写了一个插件指令,用于监听来自$rootScope的事件广播,如果播放视频,则在播放事件时自动暂停。
omgYesDirectives.directive('vgAutoPause',
['$rootScope',
function($rootScope) {
return {
restrict: 'E',
require: '^videogular',
link: function($scope, $elem, $attr, $API) {
$rootScope.$on('onGameEnable', onGameEnable);
function onGameEnable(event, data)
{
$API.pause();
}
}
}
}
]);但我想不出怎么对它进行单元测试。我似乎无法在我的测试中正确地注入视频本身。我试过这种变体:
describe('vgAutoPause', function () {
var scope, compile, elm, videogular;
beforeEach(inject(function ($compile, $rootScope, videogularDirective) {
videogular = videogularDirective;
scope = $rootScope.$new();
compile = $compile;
}));
it('should instantiate as an HTML element', function () {
elm = compile('<videogular><vg-auto-pause></vg-auto-pause></videogular>')(scope);
scope.$digest();
expect(elm.html()).toContain('vg-auto-pause');
});
});但是卡玛一直在抱怨:
Error: [$injector:unpr] Unknown provider: videogularDirectiveProvider <- videogularDirective我做错了吗?你对我该做什么有什么想法或建议吗?
发布于 2014-03-16 11:28:15
在AngularJS中,您不能注入一个指令,您必须创建$compile,然后才能启动$digest循环。
例如,这是一个简单的videogular测试:
'use strict';
describe('Directive: Videogular', function () {
var element;
var scope;
beforeEach(module('myApp'));
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope;
element = angular.element("<div><videogular><video></video></videogular></div>");
$compile(element)($rootScope);
}));
describe("videogular", function() {
it("should have videogular", function() {
scope.$digest();
expect(element.html()).toContain('<video></video>');
});
});
});也许你需要首先了解如何测试指令,那里有很多好的信息。您可以从以下链接开始:
https://stackoverflow.com/questions/22386251
复制相似问题