0

我是单元测试的新手,并且有一些基础知识。但是,我正在尝试测试一种方法。此方法调用一个函数,该函数是 oidc-client.js 的一部分。它基本上让用户登录。

规格文件


import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AuthCallbackComponent } from './auth-callback.component';
import { RouterTestingModule } from '@angular/router/testing';


fdescribe('AuthCallbackComponent', () => {
  let component: AuthCallbackComponent;
  let fixture: ComponentFixture<AuthCallbackComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule],
      declarations: [ AuthCallbackComponent ]
    })
    .compileComponents();
  }));

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

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

组件.ts

import { UserManager,User,UserManagerSettings,WebStorageStateStore} from 'oidc-client';

  manager = new UserManager(this.getClientSettings());


  ngOnInit() {
    this.authService.completeAuthentication();

  }


  completeAuthentication(): Promise<void> {
    return this.manager.signinRedirectCallback().then(user => {
      this.user = user;
      this.router.navigate(['/']);
      this.setUser.next(this.user.profile);
      this.onLogin.next(true)
    });
  }



  getClientSettings() {
    return {
      authority: environment.authority,
      client_id: environment.client_id,
      redirect_uri: environment.login,
      post_logout_redirect_uri: environment.logout,
      response_type: 'code',
      scope: 'openid profile email phone address',
      filterProtocolClaims: true,
      loadUserInfo: false,
      accessTokenExpiringNotificationTime: 60,
      silentRequestTimeout: 10000,
      includeIdTokenInSilentRenew: true,
      automaticSilentRenew: true,
      silent_redirect_uri: environment.silent_redirect
    };
  }
}

我不确定如何测试这个。当我运行测试时,我只是得到'没有响应状态'。我希望测试通过,也许还有一些关于如何测试 completeAuthentication() 的想法

4

1 回答 1

0

测试您无法控制的代码的一种工具是Sinon 的stubobject,它可以让您模拟 3rd 方依赖项的所需行为。

如果没有看到您正在尝试测试的源代码,这个问题并没有太多的问题,但我希望 Sinon 项目中的示例文档足以满足您的用例。

于 2019-05-24T14:32:03.223 回答