我有一个非常简单的指令:
class HelloDirective implements ng.IDirective{
public link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs:ng.IAttributes) => void;
public template = '<div>Hello world</div>';
public scope = {};
constructor(){
}
public static Factory()
{
var directive = (/*list of dependencies*/) =>
{
return new HelloDirective(/*list of dependencies*/);
};
directive['$inject'] = ['/*list of dependencies*/'];
return directive;
}
}
export { HelloDirective};
当我在应用程序中使用它时,这可以正常工作但是当我尝试在这样的测试中使用它时:
import angular = require('angular');
import ngMock = require('angular-mocks/ngMock');
import IAngularStatic = angular.IAngularStatic;
import {HelloDirective} from "../HelloDirective";
describe('helloDirective tests', () => {
var $compile : ng.ICompileService;
var $rootScope : ng.IRootScopeService;
var app: ng.IModule;
var helloElement: any;
beforeEach(() => {
app = angular.module('helloApp', [ngMock]);
app.directive('hello', [HelloDirective.Factory()]);
var elementHtml = '<hello/>';
inject(function (_$compile_: any, _$rootScope_: any) {
$rootScope = _$rootScope_;
$compile = _$compile_;
});
helloElement = $compile(elementHtml)($rootScope);
$rootScope.$digest();
});
it('should display helloDirective correctly', () => {
expect(helloElement.text()).toBe('Hello world');
});
});
我得到:预期&#39;&#39;成为“Hello world&#39;。
如果我使用常规html作为元素,例如
<div> Hello world</div>
而不是
<hello/>
测试工作正常,当我调试时,我看到指令工厂方法被调用。
可能出现什么问题?
非常感谢任何帮助。