【问题标题】:Jasmine how to test functions which returns httpClient observable?Jasmine 如何测试返回 httpClient observable 的函数?
【发布时间】:2021-09-03 07:16:08
【问题描述】:

我在下面的组件中有一个函数

  getProperties(data) {
    let type = this.route.snapshot.paramMap.get('type');
    const types = {
      "easy": {
        adviceEndpoint: this.apiService.getEasy(),
      }, "medium": {
        adviceEndpoint: this.apiService.getMedium(),
      }
    }
    return types[type];
  }

apiService return httpClient get,post 等方法。如上所示,我成功注入了所有依赖项。有趣的是,我可以选择一种类型并返回adviceEndpoint 和adviceFeatures,但它无法获取adviceEndpoint,它当我将其打印到控制台Object{adviceEndpoint: undefined}

时,返回如下未定义
   fdescribe('ExampleComponent', () => {
        let component: ExampleComponent;
        let fixture: ComponentFixture<ExampleComponent>;
        let el: DebugElement;
        let route: any;
        beforeEach(async(() => {
            const apiServiceSpy = jasmine.createSpyObj('ApiService',
                ["getEasy", "getMedium"]
            );
            TestBed.configureTestingModule({
                imports: [NgbModule, CommonModule, RouterModule.forRoot([])],
                declarations: [ExampleComponent],
                providers: [
                    { provide: ApiService, useValue: apiServiceSpy },
                    {
                        provide: ActivatedRoute,
                        useValue: {
                            snapshot: {
                                paramMap: convertToParamMap({ type: 'easy' })
                            }
                        }
                    }
                ]
            }).compileComponents().then(() => {
                fixture = TestBed.createComponent(ExampleComponent);
                route = TestBed.inject(ActivatedRoute);
                component = fixture.componentInstance;
                el = fixture.debugElement;
                fixture.detectChanges();
            });
        }));
        it("should return true properties", () => {
            const properties = component.getProperties();
            console.log(properties);
            //Object{adviceEndpoint: undefined}
        });
    });

【问题讨论】:

  • 据我所见,我认为let type = this.route.snapshot.paramMap.get('type'); 不会从测试中返回“简单”或“中等”,您需要模拟该路线并返回您正在测试的任何一个,简单或中等

标签: javascript angular typescript jasmine karma-runner


【解决方案1】:

我希望您不要尝试在 ExampleComponent 的测试中测试您的 ApiService 模拟。如果是这样,那么只需提供一些模拟值,以便您最终检查正确的对象

it("should return true properties", () => {
  apiServiceSpy.getEasy.and.returnValue('easy-endpoint');
   const properties = component.getProperties();
   expect(properties).toEqual({adviceEndpoint: 'easy-endpoint'});
});

【讨论】:

  • 我需要那个简单的端点来返回一个类似这样的可观察的东西。http.get(....)。我只是想检查它是否返回正确的函数
  • 你确定你需要这个吗?我们正在讨论ExampleComponents 测试。并且此代码中没有真正的 ApiService 实现,只是您的 apiServiceSpy 中的一个模拟。你可以从这个间谍返回任何东西,甚至是一个可观察的,但为了简单起见,我会用字符串测试它
  • 其实你是对的,提供字符串就足够了
猜你喜欢
  • 2017-04-07
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 2017-08-17
  • 2016-12-18
  • 2018-04-01
相关资源
最近更新 更多