【问题标题】:Test a function that calls a http service inside component Angular5测试在组件 Angular5 中调用 http 服务的函数
【发布时间】:2018-06-04 14:27:12
【问题描述】:

我需要测试这个功能。

当我点击这个功能getallproductcomponent()时,执行这个服务功能getallproductservice()并返回所有产品。请问有什么想法,如何在组件中测试两者?

  getallproductcomponent() {
    this.ws.getallproductservice().subscribe(
      item=> {
        this.item= item;
      }
    );
  }

我只能测试getallproductservice。您可以在下面看到我的脚本运行良好。如何测试getallproductcomponent() {}

it('testing',
    async(inject([ProductService], (service: ProductService) => {
        TestBed.get(MockBackend).connections.subscribe(
            (connection: MockConnection) => connection.mockRespond(new Response(
                new ResponseOptions({
                                    })
            ))
        );
        service.getallproductservice().subscribe(items => {
            expect(items[0].alarmdesc).toEqual('call');
        });
    })))

有什么想法吗?

编辑:

it('should call service.getallproductservice when getallproductcomponent', done => {
    const mock = [ {id: 1, alarmdesc: 'test12'}];
    component['ws'].getallproductservice = () => Observable.of(mock);
    spyOn(component['ws'].and.callThrough();

    component['ws'].getallproductservice ().subscribe(items => {
        expect(component['ws'].getallproductservice ).toHaveBeenCalled();
        expect(component.item).toBe(mock);
        done();
    });
    component.getallproductcomponent();
}));

结果:

1: 在component['ws'].getallproductservice = () => Observable.of(mock); 错误:

类型 '() => Observable' 不是 可分配给类型 '() => Observable'。

2:在spyOn(component['ws'].and.callThrough(); 错误:

[ts] 类型“ProductService”上不存在属性“and”。

【问题讨论】:

    标签: angular unit-testing testing jasmine karma-jasmine


    【解决方案1】:

    在您的组件中,您应该测试您的服务是否被调用。您不应该测试您的服务是否正在调用端点:这将在您的服务测试中完成。

    这意味着你只需要测试一下:

    it('should call service.getallproductservice when getallproductcomponent', done => {
      const mock = [/* mock of your objects */];
      component['ws'].getallproductservice = () => Observable.of(mock);
      spyOn(component['ws'], 'getallproductservice').and.callThrough();
    
      component['ws'].getallproductservice().subscribe(() => {
        expect(component['ws'].getallproductservice).toHaveBeenCalled();
        expect(component.item).toBe(mock);
        done();
      });
    
      component.getallproductcomponent();
    });
    

    【讨论】:

    • 谢谢回答。 TypeError: Cannot set property 'getallproductservice' of undefined 我试过了:`component['ws'].getallproductservice().subscribe(() => { console.log(component.item)...` 没有执行,没有任何反应。getallproductservice() 没有执行。
    • 您的服务必须被称为 ws,这来自您自己的代码。否则,请改用您提供的名称。
    • 当我输入ws时,Type '() => Observable<boolean>' is not assignable to type '() => Observable<Products[]>'. Type 'Observable<boolean>' is not assignable to type 'Observable<Products[]>'. Type 'boolean' is not assignable to type 'Products[]'.
    • 我的意思是写模拟而不是真实的,我的错!我编辑了我的答案
    • 当我写这样的模拟时 ` const mock = [ {id: 1, alarmdesc: 'test12'}];` 好吗?我试过了[ts] Property 'and' does not exist on type 'ProductService'.
    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多