MockService仍然会导致错误:无法读取未定义的属性“订阅”

时间:2018-09-02 06:33:11

标签: angular karma-jasmine

我是Angular测试的新手。所以,我一直在关注这个pluralsight course

我不断收到相同的错误

  

无法读取未定义的属性“ subscribe”

不管我做什么。

describe('AnnualReportComponent', () => {
   let fixture: ComponentFixture<AnnualReportComponent>;
   let mockReportService;
   let REPORTITEMS = [
     { id: 1, title: 'title 1' },
     { id: 2, title: 'title 2' },
     { id: 3, title: 'title 3' }
   ]

   beforeEach(() => {
    mockReportService = jasmine.createSpyObj('ReportService', ['getAnnualReport']);

    TestBed.configureTestingModule({
       imports: [FormsModule],
       declarations: [AnnualReportComponent], 
       providers: [
         { provide: ReportService, useValue: mockReportService }      
       ],
       schemas: [
         NO_ERRORS_SCHEMA
       ]
     });

     fixture = TestBed.createComponent(AnnualReportComponent);
     component = fixture.componentInstance;
   });  

    it('should create', () => {
      fixture.detectChanges(); 
      mockReportService.getAnnualReport.and.returnValue(of(REPORTITEMS));

        expect(component).toBeTruthy();
     });    
});

这是组件

export class AnnualReportComponent implements OnInit, OnChanges {
  ngOnInit() {
     this.getAnnualReport();
  }

   getAnnualReport () {
      this.reportService
       .getAnnualReport(this.fiscalYear)
       .subscribe(item => {
          this.reportItems = item});
   }

}

我不断收到此错误:

  

TypeError:无法读取未定义的属性“ subscribe”
  在AnnualReportComponent.getAnnualReport(webpack:/// C:/../AnnualReportComponent.component.ts?:92:13)
  在AnnualReportComponent.ngOnInit(webpack:/// C:/../AnnualReportComponent.component.ts?:57:14)

这是我关于业力的第一项工作,但无法运行。我不知道我是否错过了什么。

感谢您的帮助。

0 个答案:

没有答案