Angular 在角度单元测试中定义时未定义的对象
我正在研究我的测试环境,并试图通过在单元测试期间未定义的Angular 在角度单元测试中定义时未定义的对象,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我正在研究我的测试环境,并试图通过在单元测试期间未定义的oidc客户端解决单个用户配置文件的问题 我尝试过使之前的方法async无效,我也尝试过重新构造我的AuthService 这是我从测试组件中得到的错误: ResourcesCardComponent>应创建 失败:无法读取未定义的属性“profile” AuthService import { Injectable } from '@angular/core'; import { UserManager, User, WebStorage
oidc客户端解决单个用户配置文件的问题
我尝试过使之前的方法async
无效,我也尝试过重新构造我的AuthService
这是我从测试组件中得到的错误:
ResourcesCardComponent>应创建
失败:无法读取未定义的属性“profile”
AuthService
import { Injectable } from '@angular/core';
import { UserManager, User, WebStorageStateStore } from 'oidc-client';
import { BehaviorSubject } from 'rxjs';
import { ConfigAssetLoaderService } from '../config-asset-loader.service';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private _userManager: UserManager;
public _user: User;
public isLoggedInSubject$ = new BehaviorSubject<any>(this._user);
isLoggedIn = this.isLoggedInSubject$.asObservable();
constructor(private configService: ConfigAssetLoaderService) {
const config = {};
this.configService.loadConfiguration().subscribe(response => {
config['authority'] = response.authority;
config['client_id'] = response.client_id;
config['redirect_uri'] = response.redirect_uri;
config['scope'] = response.scope;
config['response_type'] = response.response_type;
config['loadUserInfo'] = response.loadUserInfo;
config['userStore'] = new WebStorageStateStore({store: window.sessionStorage});
config['metadata'] = {
issuer: response.issuer,
authorization_endpoint: response.authorization_endpoint,
userinfo_endpoint: response.userinfo_endpoint,
jwks_uri: response.jwks_uri,
end_session_endpoint: response.end_session_endpoint
};
config['signingKeys'] = response.signingKeys;
config['extraQueryParams'] = {
resource: response.claimsApiResourceId
};
this._userManager = new UserManager(config);
this._userManager.getUser().then(user => {
if (user && !user.expired) {
this._user = user;
this.isLoggedInSubject$.next(user);
}
});
});
}
}
以下是ResourceCardComponent
的测试组件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ResourcesCardComponent } from './resources-card.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { ResourcesCardContainerComponent } from './resources-card-container/resources-card-container.component';
const fakeRoute = {
snapshot: {
data: {
actionLinks: []
}
}
};
describe('ResourcesCardComponent', () => {
let component: ResourcesCardComponent;
let fixture: ComponentFixture<ResourcesCardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ResourcesCardComponent,
ResourcesCardContainerComponent
],
imports: [
RouterModule,
HttpClientTestingModule
],
providers: [
{
provide: ActivatedRoute,
useFactory: () => fakeRoute
}
]
}).compileComponents();
}));
beforeEach(async(() => {
fixture = TestBed.createComponent(ResourcesCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
component.actionLinks = [];
expect(component).toBeTruthy();
});
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“./resources card.component”导入{resourcescarddcomponent};
从'@angular/common/http/testing'导入{HttpClientTestingModule};
从“@angular/router”导入{ActivatedRoute,RouterModule};
从“./resources-card-container/resources-card-container.component”导入{ResourcesCardContainerComponent};
常数fakeRoute={
快照:{
数据:{
actionLinks:[]
}
}
};
描述('ResourcesCardComponent',()=>{
let组件:ResourcesCardComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[
资源共享组件,
资源共享容器组件
],
进口:[
路由模块,
HttpClientTestingModule
],
供应商:[
{
提供:激活的路由,
useFactory:()=>fakeRoute
}
]
}).compileComponents();
}));
beforeach(异步(()=>{
fixture=TestBed.createComponent(ResourcesCardComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
}));
它('应该创建',()=>{
component.actionLinks=[];
expect(component.toBeTruthy();
});
});
您使用
public isLoggedInSubject$ = new BehaviorSubject<any>(this._user)
我完全意识到这是一个错误,我正试图找出如何避免这个错误。有什么想法吗?
public isLoggedInSubject$ = new BehaviorSubject<any>(this._user)
spyOn(authService , 'isLoggedInSubject$').and.returnValue(of({profile:{unique_name:'some name'}}))