[英]Angular unit test: Property subscribe does not have access type get
Trying to run unit test of observable property when value comes with type error.当值带有类型错误时,尝试运行可观察属性的单元测试。
Running the test I get: Error: Property subscribe does not have access type get运行我得到的测试:错误:属性订阅没有访问类型获取
What am I doing wrong?我究竟做错了什么? Thank you for the help.
感谢您的帮助。
app-list.component.ts应用程序列表.component.ts
export class TransferComponent {
transfers$: Observable<Transfer[] | AppResponseError>;
constructor(
private transfersService: TransfersService) {
this.transfers$ = this.transfersService.transfers$;
}
}
app-list.component.spec.ts应用程序列表.component.spec.ts
it('should hide app-checklist component when return AppResponseError', () => {
const error : AppResponseError = {
code:'400',
message:'Bad request'
};
spyOnProperty(component.transfers$, 'subscribe').and.returnValue(of(error));
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('app-checklist'))).toBeNull();
});
spyOnProperty
is intended for use on objects that have usedObject.defineProperty
since that is implemented with a function behind it and can't be modified by just assigning to it.spyOnProperty
旨在用于已使用Object.defineProperty
的对象,因为它是通过 function 实现的,并且不能通过仅分配给它来修改。 If you just have basic properties on an object, you should be able to just assign them.如果您只有 object 的基本属性,您应该可以分配它们。
You can just assign a stub value to component.transfers$
property.您可以只为
component.transfers$
属性分配一个存根值。
Eg例如
component.ts
: component.ts
:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { TransfersService } from './TransfersService';
export type AppResponseError = any;
type Transfer = any;
@Component({
selector: 'app-transfer',
})
export class TransferComponent implements OnInit {
transfers$: Observable<Transfer[] | AppResponseError>;
constructor(private transfersService: TransfersService) {
this.transfers$ = this.transfersService.transfers$;
}
ngOnInit() {
this.transfers$.subscribe((data) => {
console.log(data);
});
}
}
TransferService.ts
: TransferService.ts
:
import { Injectable } from '@angular/core';
import { of } from 'rxjs';
@Injectable()
export class TransfersService {
transfers$ = of('real observable');
}
component.test.ts
: component.test.ts
:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { AppResponseError, TransferComponent } from './component';
import { TransfersService } from './TransfersService';
fdescribe('65208911', () => {
let component: TransferComponent;
let fixture: ComponentFixture<TransferComponent>;
const transfersServiceStub = {
transfers$: of(),
};
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TransferComponent],
providers: [
{ provide: TransfersService, useValue: transfersServiceStub },
],
});
fixture = TestBed.createComponent(TransferComponent);
component = fixture.componentInstance;
});
it('should hide app-checklist component when return AppResponseError', () => {
const error: AppResponseError = {
code: '400',
message: 'Bad request',
};
component.transfers$ = of(error);
fixture.detectChanges();
// expect(fixture.debugElement.query(By.css('app-checklist'))).toBeNull();
});
});
unit test result:单元测试结果:
LOG: Object{code: '400', message: 'Bad request'}
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 0 of 15 SUCCESS (0 secs / 0 secs)
WARN: 'Spec '65208911 should hide app-checklist component when return AppResponseError' has no expectations.'
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 0 of 15 SUCCESS (0 secs / 0 secs)
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 1 of 15 (skipped 14) SUCCESS (0.086 secs / 0.026 secs)
TOTAL: 1 SUCCESS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.