我在Angular 7中具有Component,它具有构造函数参数的作用。
@Component({
selector: 'cc-schedule-list',
templateUrl: './schedule-list.component.html',
styleUrls: ['./schedule-list.component.scss']
})
export class ScheduleListComponent implements OnInit {
constructor(public accountActivityService: AccountActivityService) {}
ngOnInit(): void {
}
}
BaseService
@Injectable({
providedIn: 'root'
})
export class BaseService {
public commonHeaders: CommonHeaderModel;
public legacyData: any;
constructor(
public httpClient: HttpClient,
public appService: AppService
) {
this.legacyData = this.appService.getDataFromLegacy();
this.commonHeaders = {
'sourceRequestID': 'KBB',
'uuid': this.legacyData.UUID,
};
}
getData(url: string, serviceHeaders: any, params?: any) {
const headers = {...this.commonHeaders, ...serviceHeaders};
return this.httpClient.get(url, {headers: headers, params: params});
}
}
AccountActivityService
@Injectable({
providedIn: 'root'
})
export class AccountActivityService {
constructor(public base: BaseService) {
}
/**
* getPendingPayments Function gets the all the pending payment transactions
*
* @returns All transactions on the account
*/
getPendingPayments(electronicCardIdentifier, payeeAccountIdentifier) {
return null; //return null at present
}
}
ScheduleListComponent.spec文件
describe('ScheduleListComponent', () => {
let component: ScheduleListComponent;
let fixture: ComponentFixture<ScheduleListComponent>;
let injector: any;
let debugElement: DebugElement;
let accountActivityService: MockAccountActivityService;
/** Mock Account Activity Service ***/
class MockAccountActivityService extends AccountActivityService {
getPendingPayments() {
return null;
}
}
beforeEach( (() => {
TestBed.configureTestingModule({
declarations: [ ScheduleListComponent],
imports: [
...
],
providers: [ UserUtilService, { provide: AccountActivityService, useClass: MockAccountActivityService}],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
fixture = TestBed.createComponent(ScheduleListComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
accountActivityService = debugElement.injector.get(MockAccountActivityService);
}));
it('should create', () => {
expect(component).toBeDefined();
});
});
我总是会收到此错误***角度测试无法读取未定义或空引用的属性“ UIID”。
可能是我的ScheduleListComponent没有将MockAccountActivityService作为构造函数参数。请在这里帮助我
您可以嘲笑您的服务:{ provide: AccountActivityService, useValue: {}}
尝试这样做:
ScheduleListComponent.spec文件
describe('ScheduleListComponent', () => {
let component: ScheduleListComponent;
let fixture: ComponentFixture<ScheduleListComponent>;
beforeEach( (() => {
TestBed.configureTestingModule({
declarations: [ ScheduleListComponent],
imports: [
...
],
providers: [ UserUtilService, { provide: AccountActivityService, useValue:{}}],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
fixture = TestBed.createComponent(ScheduleListComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
}));
it('should create', () => {
expect(component).toBeDefined();
});
});
尝试一下是否可行。