【问题标题】:Angular4 - how to test a serviceAngular4 - 如何测试服务
【发布时间】:2018-03-29 00:35:38
【问题描述】:

我是 Angular4 的新手,需要为我构建的一个简单服务编写单元测试,但不知道从哪里开始。

该服务只是简单地包装了一个 api 调用,如下所示:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';

import { KeyValuePair } from '../../models/keyvaluepair';
import { environment } from './../../../environments/environment';

// Lookups api wrapper class.
@Injectable()
export class LookupsService {

    private servicegApiUrl = '';

    public constructor( private http : HttpClient ) {  
        // Build the service api url, uring the environment lookups api url, plus controller name to reference.
        this.servicegApiUrl = environment.webApiUrl + 'Lookups/';
    }

    // Get the hubs from the web api.
    public getHubs() :  Observable<KeyValuePair<number>[]> {
        // Carry out http get and map the result to the KeyValuePair number object.
        return this.http.get<KeyValuePair<number>[]>(this.servicegApiUrl + 'Hubs').map(res => { return res; });
    }
}

我需要测试我的 getHubs() 方法但不知道如何。此外,我在网上看到了各种关于测试服务的文章,但我不确定我是否必须模拟预期的结果,或者这是否应该实际调用 Web 服务。我有这段代码,但 expect 似乎永远不会被执行:

import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { LookupsService } from './Lookups.Service';
import { KeyValuePair } from './../../models/KeyValuePair';

describe(`LookupsService tests`, () => {

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [
        HttpClientModule,
        HttpClientTestingModule
      ],
      providers: [
        LookupsService
      ]
    });
  });



  it(`should get results from the web method`, async(inject( [ LookupsService, HttpTestingController ],

    (service: LookupsService, backend: HttpTestingController) => {

      service.getHubs().subscribe((hubs : KeyValuePair<number>[]) => {
        // This code never seems to run...
        console.log(hubs.length);
        expect(hubs.length).toBeGreaterThan(0);
      });
  })));

});

为了完整性,KeyValuePair 类如下所示:

export class KeyValuePair<T> {
    Key : string;
    Value : T;
}

非常感谢任何帮助!

【问题讨论】:

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


    【解决方案1】:

    单元测试应该(至少在大多数情况下)在不使用真正的 API 或数据库调用的情况下测试您的代码。

    一种方法是模拟用于处理真实数据的库/依赖项/部分代码。

    在您的情况下,您可以创建一个 FakeHttpClient 服务来处理您的虚假数据,并在您测试您的 LookupService 服务时使用它。初始化会是这样的:

    var lookupService = new LookupService(new FakeHttpClientService());
    //rest of your code for testing LookupService class.
    

    您可以找到更多详细信息here

    【讨论】:

    • 好酷 - 所以最好在不依赖真正的 api 时进行测试。所以我嘲笑做实际的电话。有没有人用 HttpClient 作为模拟结果的依赖项来测试服务的好例子?
    • 您可以随时尝试使用spyOn 来监视某个方法。检查此 SO 答案以了解用法:stackoverflow.com/questions/30658525/…
    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 2017-10-14
    • 2012-11-23
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多