单元测试-访问订阅功能

问题描述 投票:0回答:1

[嗨,我正在尝试为具有可观察性的组件编写角度代码,但无法访问订阅功能。 我应该如何访问订阅功能以测试其中的代码?任何帮助将不胜感激。谢谢。

这是我的组件,具有可观察的:

  ngOnInit(): void {

    if (this.authService.getUser() !== null || this.authService.getUser() !== undefined) {
      this.userService.userDetails = this.authService.getUser();
    }

    if (this.environmentName === 'QA' || this.environmentName === 'LOCAL' || this.environmentName === 'QA-STAGING') {
      this.authService.acquireTokenSilent(['api://012fdc3a-c966-4312-9b5c-301f097c1803/server']);
    } else {
      this.authService.acquireTokenSilent(['api://58a80bb5-906b-4ec0-9b41-7a78a07125af/server']);
    }

    this.subscription.add(
      this.broadcastService.subscribe('msal:acquireTokenSuccess', (payload) => {
        console.log('acquire token success ' + JSON.stringify(payload));
        this.roleService.checkServerEventReviewers().subscribe(res => {
          this.userService.userDetails.role = res ? 'Data Steward' : 'Mosaic Consumer';
          if (this.isLoggedIn !== true) {
            const redirectUri = sessionStorage.getItem('redirectUri');
            if (redirectUri !== undefined || redirectUri !== null) {
              this.router.navigateByUrl(redirectUri);
            }
          }
          this.isLoggedIn = true;
};

这是我正在尝试的规格文件:

describe('AppComponent', () => {
  beforeEach(() => {
    let subscription: Subscription = new Subscription();
    TestBed.configureTestingModule({
      imports: [RouterTestingModule],
      declarations: [AppComponent],
      providers: [WindowService, RoleService, HttpClient, HttpHandler, BroadcastService, MsalService,
        {
          provide: MSAL_CONFIG,  // MsalService needs config, this provides it.
          useFactory: () => ({   // Note this is an arrow fn that returns the config object
            redirectUri: window.location.origin + '/',
            clientID: mockData.clientID,
          }),
        }],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    }).compileComponents();
  });

  describe(':', () => {
    function setup() {
      const fixture = TestBed.createComponent(AppComponent);
      const app = fixture.debugElement.componentInstance;
      const compiled = fixture.debugElement.nativeElement;
      return {fixture, app, compiled};
    }

    it('Init with QA environment', () => {
      const {app} = setup();
      spyOn(app.authService, 'getUser').and.returnValue(mockData.userDetails);
      spyOn(app.authService, 'acquireTokenSilent').and.returnValue('msal:acquireTokenSuccess');
      app.ngOnInit();
      app.subscription.add(
        app.broadcastService.subscribe('msal:acquireTokenSuccess', () => {
        // do something here
        });
    );
angular unit-testing angular7 subscription
1个回答
0
投票

有两种方法可以做到:

  1. 您可以提取功能并将其作为功能本身进行测试

声明如下:

 function handlePayload(payload) {
    console.log('acquire token success ' + JSON.stringify(payload));
    this.roleService.checkServerEventReviewers().subscribe(res => {
      this.userService.userDetails.role = res ? 'Data Steward' : 'Mosaic Consumer';
      if (this.isLoggedIn !== true) {
        const redirectUri = sessionStorage.getItem('redirectUri');
        if (redirectUri !== undefined || redirectUri !== null) {
          this.router.navigateByUrl(redirectUri);
        }
      }
      this.isLoggedIn = true;
}

和ngOnInit

this.subscription.add(
  this.broadcastService.subscribe('msal:acquireTokenSuccess', this.handlePayload));

所以现在您可以直接测试句柄有效负载

  1. 使用间谍模拟this.broadcastService以使用of函数返回可观察对象。

这样,您将通过调用ngOnInit来测试订阅的结果。

© www.soinside.com 2019 - 2024. All rights reserved.