无法使用注入的angularjs服务测试角度管道
AngularJS服务:无法使用注入的angularjs服务测试角度管道,angular,angular-services,angular-hybrid,Angular,Angular Services,Angular Hybrid,AngularJS服务: app.service('NameService', function() { this.name = function (name) { return "Hello " + name; } }); 角度升级: import { InjectionToken } from "@angular/core"; export const NameService = new InjectionToken<any&g
app.service('NameService', function() {
this.name = function (name) {
return "Hello " + name;
}
});
角度升级:
import { InjectionToken } from "@angular/core";
export const NameService = new InjectionToken<any>('NameService');
export function nameServiceFactory(i: any): any {
return i.get('NameService');
}
export const nameServiceProvider = {
provide: NameService,
useFactory: nameServiceFactory,
deps: ['$injector']
};
我在测试这根管子时遇到了问题
describe('NamePipe', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
providers: [nameServiceProvider],
declarations: [NamePipe]
})
.compileComponents();
}));
it('create an instance', () => {
const service: any = TestBed.get(NameService);
const pipe = new NamePipe(service);
expect(pipe).toBeTruthy();
});
});
这会引发以下错误:
NullInjectorError:没有$injector的提供程序
我在这里尝试过查看createAngularJSTestingModule和createAngularTestingModule,但没有任何运气
一旦我将AngularJS模块拉入测试,我就会发现错误,即AngularJS模块中没有加载程序来处理.html文件类型
如何将升级的AngularJS服务注入角度测试组件
describe('NamePipe', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
providers: [nameServiceProvider],
declarations: [NamePipe]
})
.compileComponents();
}));
it('create an instance', () => {
const service: any = TestBed.get(NameService);
const pipe = new NamePipe(service);
expect(pipe).toBeTruthy();
});
});