Javascript Angular 4测试用例.then()不是函数

Javascript Angular 4测试用例.then()不是函数,javascript,angular,jasmine,karma-jasmine,Javascript,Angular,Jasmine,Karma Jasmine,我试图用下面描述的测试用例覆盖组件中的服务调用。 但运行ng测试时出错,如下所示: 失败:this.monthService.getMonthView(…)。则不是函数 TypeError:this.monthService.getMonthView(…)。then不是一个函数 服务:(MonthService.ts) getMonthView是MonthService getMonthView(forecastMonth: string): Promise<any[]> {

我试图用下面描述的测试用例覆盖组件中的服务调用。 但运行
ng测试时出错,如下所示:

失败:this.monthService.getMonthView(…)。则不是函数 TypeError:this.monthService.getMonthView(…)。then不是一个函数

服务:(MonthService.ts)

getMonthView
MonthService

 getMonthView(forecastMonth: string): Promise<any[]> {
        const options = new RequestOptions({ headers: this.headers });
        const url = this.BaseUrl + forecastMonth;
        return this.http.get(url, options)
            .toPromise()
            .then(response => response.json() as any[])
            .catch(this.handleError);
    }
测试套件

 beforeEach(() => {
        /**
          * Ref:https://angular.io/guide/testing#!#component-fixture
          */
        fixture = TestBed.createComponent(MonthComponent);
        component = fixture.componentInstance;        
        myService = TestBed.get(MonthService );        
        //fixture.detectChanges();
    });
    it('should be created', () => {
        expect(component).toBeTruthy();
    }); 

    fit('should test loadDataOnMonthViewClick', async(() => { 
        let data=["10","20","30"];
        spyOn(component.monthService, 'getMonthView').and.returnValue(result);        
        component.loadDataOnMonthViewClicked('Jan');
        fixture.detectChanges();
        expect(component.result).toBe(data);
    }));

这是我为让它工作所做的

// the component

import { Component, OnInit } from "@angular/core";
import { MonthService } from "./month.service";

@Component({
  selector: "app-root",
  template: "",
})
export class AppComponent {
  result = undefined;
  constructor(private monthService: MonthService) { }

  loadDataOnMonthViewClicked(forecastMonth) {
    this.monthService
      .getMonthView(forecastMonth)
      .then(response => (this.result = response));
  }
}
服务呢

// the service

import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import "rxjs/add/operator/toPromise";

@Injectable()
export class MonthService {
  constructor(private http: Http) { }

  getMonthView(_forecastMonth: string) {
    return this.http
      .get("http://jsonplaceholder.typicode.com/posts/1")
      .toPromise()
      .then(response => response.json())
      .catch(console.error);
  }
}
测试呢

import { TestBed, async, ComponentFixture } from "@angular/core/testing";
import { AppComponent } from "./app.component";
import { MonthService } from "./month.service";
import { HttpModule } from "@angular/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";

describe("AppComponent", () => {
  let fixture: ComponentFixture<AppComponent>;
  let component: AppComponent;
  let myService;

  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        providers: [MonthService],
        declarations: [AppComponent],
        imports: [HttpModule],
      }).compileComponents();
    }),
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    myService = fixture.debugElement.injector.get(MonthService);
    fixture.detectChanges();
  });

  it(
    "should test loadDataOnMonthViewClick",
    async(() => {
      let data = ["10", "20", "30"];
      spyOn(myService, "getMonthView").and.callFake(() =>
        Promise.resolve(data),
      );
      component.loadDataOnMonthViewClicked("Jan");
      fixture.whenStable().then(() => {
        expect(component.result).toBe(data);
      });
    }),
  );
});
import { TestBed, async, ComponentFixture } from "@angular/core/testing";
import { AppComponent } from "./app.component";
import { MonthService } from "./month.service";
import { HttpModule } from "@angular/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";

describe("AppComponent", () => {
  let fixture: ComponentFixture<AppComponent>;
  let component: AppComponent;
  let myService;

  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        providers: [MonthService],
        declarations: [AppComponent],
        imports: [HttpModule],
      }).compileComponents();
    }),
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    myService = fixture.debugElement.injector.get(MonthService);
    fixture.detectChanges();
  });

  it(
    "should test loadDataOnMonthViewClick",
    async(() => {
      let data = ["10", "20", "30"];
      spyOn(myService, "getMonthView").and.callFake(() =>
        Promise.resolve(data),
      );
      component.loadDataOnMonthViewClicked("Jan");
      fixture.whenStable().then(() => {
        expect(component.result).toBe(data);
      });
    }),
  );
});
providers: [
  { provide: FetchDataService, useClass: FetchDataServiceMock }
],