I am trying to write some tests for my Angular application. I am writing my tests in Jasmin / Karma / PhantomJS and I keep getting an error which makes all my tests fail, this is Error: No provider for SessionUtil!
SessionUtil is a Utility Class / Service that has numerous public methods. in my main.component.ts file I inject it into my construtor like so
constructor(@Inject(SessionUtil) private sessionUtil: SessionUtil)
and this is my test file (not all of it, just where I declare everything and import my services, etc)
describe('MainComponent', () => {
let componentFixture: ComponentFixture<MainComponent>;
let instance: any;
let element: any;
let sessionUtil: SessionUtil;
let spyLocalizationsService;
const firstLanguage = 'en-US';
const secondLanguage = 'de-DE';
const unknownLanguage = 'xx-XX';
beforeEach(async(() => {
spyLocalizationsService = sinon.createStubInstance(LocalizationsService);
spyLocalizationsService.changeLanguage.withArgs(firstLanguage, sinon.match.any, sinon.match.any).callsArg(1);
spyLocalizationsService.changeLanguage.withArgs(secondLanguage, sinon.match.any, sinon.match.any).callsArg(1);
spyLocalizationsService.changeLanguage.withArgs(unknownLanguage, sinon.match.any, sinon.match.any).callsArg(2);
spyLocalizationsService.getSupportedLanguages.returns([firstLanguage, secondLanguage]);
(sinon.stub(spyLocalizationsService, 'currentLanguage') as any).get(() => firstLanguage);
// Allows overriding default providers, directives, pipes
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
// some Paths
]),
TranslateI18NextTestingModule.forRoot(),
AuthTestingModule.forRoot(),
NoopAnimationsModule,
MaterialModule
],
declarations: [
MainComponent,
DummyComponent
],
schemas:
[CUSTOM_ELEMENTS_SCHEMA],
providers:
[{provide: LocalizationsService, useValue: spyLocalizationsService},
{provide: MATERIAL_COMPATIBILITY_MODE, useValue: true}]
}).compileComponents().then(() => {
componentFixture = TestBed.createComponent(MainComponent);
element = componentFixture.nativeElement;
instance = componentFixture.componentInstance; // BannerComponent test instance
sessionUtil = componentFixture.debugElement.injector.get(SessionUtil);
});
}));
const isAuthenticated = () => Promise.resolve({
isAuthenticated: true,
username: 'Max Musterman'
});
describe('on init', () => {
beforeEach(async(() => {
sinon.stub(sessionUtil, 'isAuthenticated').returns(isAuthenticated());
}));
when I run my tests I just get the following error and none of my tests run, Uncaught (in promise): Error: No provider for SessionUtil!
I am obviously doing something wrong. I tried to inject the SessionUtil
in the TestBed.configureTestingModule
object providers
array, but this gave me just as many problems. Can anyone see how I am injecting this incorrectly?
Any advice would be appreciated!
{provide: SessionUtil, useValue: sessionUtil}
而且您需要使用存根启动sessionUtil
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.