【发布时间】:2022-12-02 03:33:00
【问题描述】:
I have a Test in Angular that looks like this. But it always fails because I cannot mock the MsalService correctly.
export class MockedMsalService extends MsalService {}
describe('NavbarComponent', () => {
let component: NavbarComponent;
let fixture: ComponentFixture<NavbarComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [NavbarComponent],
imports: [
],
providers: [
{ provide: MsalService, useClass: MockedMsalService },
{ provide: Router, useClass: RouterMock },
{ provide: UsersService, useClass: UsersServiceMock },
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(NavbarComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
When I try to run the test I get the error:NullInjectorError: R3InjectorError(DynamicTestModule)[MsalService -> InjectionToken MSAL_INSTANCE -> InjectionToken MSAL_INSTANCE]: NullInjectorError: No provider for InjectionToken MSAL_INSTANCE!
I would be very grateful if someone could help me further
【问题讨论】:
-
Post the code for the component you are testing.
-
Also is MSAL coming from a module you've forgotten to import?
-
I should probably also point out that
export class MockedMsalService extends MsalService {}isn't mocking anything. It's providing an identical class.
标签: angular typescript jestjs msal