Reputation: 24583
AngularJS Service:
app.service('NameService', function() {
this.name = function (name) {
return "Hello " + name;
}
});
Angular Upgrade:
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']
};
Now I can use that service in am Angular component or pipe:
@Pipe({
name: 'name',
pure: false
})
export class NamePipe implements PipeTransform {
constructor(@Inject(NameService) private nameService: any) { }
transform(value: any, args?: any): any {
// can use this.nameService here
}
}
I am running into problems with testing this pipe.
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();
});
});
This throws the following error:
NullInjectorError: No provider for $injector!
I have tried looking here https://angular.io/api/upgrade/static/testing/ at createAngularJSTestingModule and createAngularTestingModule without any luck.
As soon as I pull in my AngularJS module into the test I get errors that there is no loader to handle .html file types that live in the angularJS module.
How do I inject an Upgraded AngularJS service into an Angular test component?
Upvotes: 1
Views: 77