mercredi 17 mai 2017

Angular Testing: Expected undefined to equal 204

I am new to Angular and it's testing.I would like to use MonkService way to simulate the response data returned from service, but I do not know how to do it.i have tried this but failed.But i caught this error. Expected undefined to equal 204.

//part of code in strategyTable.component.ts
onClickDelete(strategy:Strategy):void{
      this.strategyService.deleteStrategyById(strategy.id).subscribe((res:Response)=>{
          this.status = res.status;
          alert("delete success!");
          //location.reload();
        },
        error=>{alert("delete failed!");console.log(error);},
        ()=>console.log(strategy.strategyname +" This object has been deleted"));
   
  }
//strategyTable.component.spec.ts
class MockStrategyTablesService extends StrategyTablesService{
  //noinspection JSAnnotator
  deleteStrategyById(id: number){
    var mockData={
      _body:null,
      status:204,
      statusText:"OK",
      type:2,
      url:"http://localhsot:8080/api/strategys/10"
    }
    return Observable.of({
      Response:() => mockData
    });
  };
}

describe('strategyTable.component',()=>{

  let compp;
  let link = new Links();
  let strategy =new Strategy();


  beforeEach(()=>{
    TestBed.configureTestingModule({
      imports:[HttpModule,RouterTestingModule],
      providers:[
        StrategyTables,
        {provide:StrategyTablesService,useClass:MockStrategyTablesService},
        //{provide:Router,useClass:RouterStub},
        Location,
      ]
    });


  });

   beforeEach(inject([StrategyTables],s => {
   compp = s;
   }));

  it('test delete',async(()=>{
    compp.onClickDelete(strategy);
    expect(compp.status).toEqual(204);
  }));

});

Does anyone know where the problem may be? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire