angular - this.strategyService.getAllstrategys(...).subscribe 不是函数

标签 angular testing karma-runner

我在对 Angular Testing 进行测试时发现了这个错误。以下是我的代码的一部分。 失败:this.strategyService.getAllstrategys(...).subscribe 不是函数 TypeError: 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);
   }));

});

最佳答案

getAllstrategys 的模拟实现返回一个普通对象而不是 Observable。您可以使用 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
    });
}

关于angular - this.strategyService.getAllstrategys(...).subscribe 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970904/

相关文章:

ruby-on-rails - 在 rails 中测试的正确方法

rest - 在哪里为前端/后端应用程序编写测试?

git - 何时使用 "chore"作为提交消息的类型?

ng-inline-svg 的 Angular2 异步测试问题

javascript - 在 Angular 2 的字符串插值表达式中使用 Elvis 运算符

html - 如何使 ng2-charts 垂直条形图水平滚动?

Angular 2 Material : sidenav toggle from another component

angular - 引用错误: IDBIndex is not defined in angular universal app

Java JUnit 测试未通过

jquery - Ajax : empty json response when using Karma