【问题标题】:Mock MsalService in Angular JestMock MsalService in Angular Jest
【发布时间】: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


【解决方案1】:

The right answer is in the comment: MockedMsalService extends MsalService doesn't mock MsalService.

To avoid dependency hassle, the best way is to use a mocking library, for example ng-mocks.

In this case, your test can look like:

 describe('NavbarComponent', () => {
  let component: NavbarComponent;
  let fixture: ComponentFixture<NavbarComponent>;

  // NavbarComponent stays as it is
  // All dependencies in NavbarModule will be mocks
  beforeEach(() => MockBuilder(NavbarComponent, NavbarModule));

  beforeEach(() => {
    fixture = TestBed.createComponent(NavbarComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

And to customize mocks, you can use MockInstance or MockBuilder.mock().

【讨论】:

    【解决方案2】:

    You can provide all factories wich were declared into your app.module.ts

        import {
           MsalGuard,
           MsalService,
           MSAL_GUARD_CONFIG,
           MSAL_INSTANCE,
         } from '@azure/msal-angular';
    
         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 },
                {
                  provide: MSAL_INSTANCE,
                  useFactory: MSALFactory, // your app.module.ts factory
                },
                {
                  provide: MSAL_GUARD_CONFIG,
                  useFactory: MSALGuardConfigFactory, // your app.module.ts guard factory
                },
                 MsalService,
                 MsalGuard,
              ],
              schemas: [NO_ERRORS_SCHEMA],
            }).compileComponents();
          });
        
          beforeEach(() => {
            fixture = TestBed.createComponent(NavbarComponent);
            component = fixture.componentInstance;
            fixture.detectChanges();
          });
        
          it('should create', () => {
            expect(component).toBeTruthy();
          });
        });
    

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2020-02-25
      • 2021-10-11
      • 2019-06-21
      • 2020-04-02
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      相关资源
      最近更新 更多