2017-05-15 5 views
0

私は角テストのテストを行ったときにこのエラーを検出しました。フォローは私のコードの一部です。 失敗しました:。this.strategyService.getAllstrategys(...)購読するには、機能 TypeError例外ではありません:。this.strategyService.getAllstrategys(...)購読されていない機能this.strategyService.getAllstrategys(...)。subscribeは関数ではありません

//In strategyTables.component.ts 
 
getStrategys():void{ 
 
    this.strategyService.getAllstrategys(this.strategyname,this.isuseragent,this.iscookie,this.currentpage,this.itemsPerPage,this.sorts).subscribe(data=>{ 
 
     this.totalItems=data.json().totalElements; 
 
     this.currentPage=data.json().number+1; 
 
     this.strategys=data.json().content; 
 
     for(var i=0;i<data.json().totalPages;i++){ 
 
      this.totalnumbers[i]=i+1; 
 
     } 
 
     }, 
 
     error => console.log(error),()=>console.log("获取到所有的urlmng")); 
 
    }

//In strategyTables.component.spec.ts 
 
class MockStrategyTablesService extends StrategyTablesService{ 
 
    //noinspection JSAnnotator 
 
    getAllstrategys(strategyname:string ,isuseragent:string ,iscookie:string ,page:number, 
 
        size:number,sorts:string){ 
 
    return{ 
 
     "content" : [ { 
 
     "id" : 11, 
 
     "strategyname" : "strategy11", 
 
     "isuseragent" : "是", 
 
     "depth" : 2, 
 
     "downloadDelay" : 3, 
 
     "iscookie" : "yes", 
 
     "agent" : null, 
 
     "starttime" : "" 
 
     }, { 
 
     "id" : 10, 
 
     "strategyname" : "策略", 
 
     "isuseragent" : "是", 
 
     "depth" : 2, 
 
     "downloadDelay" : 2, 
 
     "iscookie" : "否", 
 
     "agent" : "2", 
 
     "starttime" : "2017年2月2日 14:44:47" 
 
     }, { 
 
     "id" : 9, 
 
     "strategyname" : "策略9", 
 
     "isuseragent" : "是", 
 
     "depth" : 3, 
 
     "downloadDelay" : 5, 
 
     "iscookie" : "否", 
 
     "agent" : "1", 
 
     "starttime" : "2017年2月2日 14:41:56" 
 
     }, { 
 
     "id" : 6, 
 
     "strategyname" : "策略6", 
 
     "isuseragent" : "是", 
 
     "depth" : 10, 
 
     "downloadDelay" : 3, 
 
     "iscookie" : "否", 
 
     "agent" : null, 
 
     "starttime" : "2017-03-09 15:08:56" 
 
     }, { 
 
     "id" : 5, 
 
     "strategyname" : "策略5", 
 
     "isuseragent" : "是", 
 
     "depth" : 10, 
 
     "downloadDelay" : 3, 
 
     "iscookie" : "否", 
 
     "agent" : null, 
 
     "starttime" : "2017-03-01 15:08:53" 
 
     } ], 
 
     "last" : false, 
 
     "totalPages" : 2, 
 
     "totalElements" : 8, 
 
     "size" : 5, 
 
     "number" : 0, 
 
     "sort" : [ { 
 
     "direction" : "DESC", 
 
     "property" : "id", 
 
     "ignoreCase" : false, 
 
     "nullHandling" : "NATIVE", 
 
     "ascending" : false 
 
     } ], 
 
     "first" : true, 
 
     "numberOfElements" : 5 
 
    } 
 
    } 
 
} 
 
describe('strategyTable.component',()=>{ 
 

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

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

 
    it('test',async(()=>{ 
 
    compp.getStrategys(); 
 
    expect(compp.totalItems).toEqual(8); 
 
    })); 
 

 
});

+0

あなたは私にいくつかの具体的なアドバイスを与えることができ、私は理解していない申し訳ありませんが、 'getAllstrategys' – Pengyy

+0

から観察可能に返却されていない、私はちょうどきました? 2日間このことを学んだ –

答えて

0

getAllstrategysのあなたのモック実装は0の代わりにプレーンなオブジェクトを返します。。あなた簡単にObservable.ofを使用してObservableにプレーンなオブジェクトを変換することができます

import { Observable } from "rxjs/Rx"; 
// ... 

getAllstrategys(strategyname: string, isuseragent: string, iscookie: string, page: number, 
    size: number, sorts: string) { 
    var mockData = { 
     "content": [{ 
      "id": 11, 
      "strategyname": "strategy11", 
      "isuseragent": "是", 
      "depth": 2, 
      "downloadDelay": 3, 
      "iscookie": "yes", 
      "agent": null, 
      "starttime": "" 
     }, 
     // ...  
     ], 
     "last": false, 
     "totalPages": 2, 
     "totalElements": 8, 
     "size": 5, 
     "number": 0, 
     "sort": [{ 
      "direction": "DESC", 
      "property": "id", 
      "ignoreCase": false, 
      "nullHandling": "NATIVE", 
      "ascending": false 
     }], 
     "first": true, 
     "numberOfElements": 5 
    } 
    return Observable.of({ 
     json:() => mockData 
    }); 
} 
+0

ありがとう!それは動作します –

+0

そしてあなたは、MockServiceの応答データをシミュレートする方法を知っていますか、ありがとう –

関連する問題