由于构造函数,Angular 4 unittest覆盖率不是100%

时间:2017-11-15 08:08:57

标签: angular unit-testing code-coverage

我注意到我的Angular 4项目的覆盖范围不是100%,因为构造函数存在问题。我使用默认的angular-cli设置。在覆盖率报告中,它显示构造函数总共被调用了19次,但仍然标记为未经测试。因此,并非所有分支都经过测试。

我的代码是否有任何问题?覆盖率报告中是否有错误?

我的代码:

import { Injectable } from '@angular/core';
import { RequestService } from '../shared/request.service';

@Injectable()
export class ProfileService {

    private customerType;
    private profiles;
    private features;

    constructor(private requestService: RequestService) { }

    private setSession(response): void {
        response = (response.json) ? response.json() : JSON.parse(response);
        const profiles = (response.profiles) ? response.profiles : response;
        profiles.forEach((profile) => {
            if (this.features) {
                this.features = this.features.concat(profile.features);
            } else {
                this.features = profile.features;
            }
        });

        // remove duplicates
        this.features = Array.from(new Set(this.features));
        this.profiles = profiles;
    }

    private getProfile(): Promise<any> {
        return this.requestService.profilesUsingGET()
            .then((response) => {
                this.setSession(response);
            }).catch(() => {

            });
    }

    public getSession(): Promise<any> {
        return this.getProfile();
    }

    public hasFeature(feature: string): boolean {
        if (this.features) {
            return this.features.indexOf(feature.toUpperCase()) !== -1;
        }
        return false;
    }

    public getFullName(): string {
        if (this.profiles && this.profiles[0]) {
            return this.profiles[0].fullName;
        }
        return '';
    }
}

单元测试:

import { TestBed, inject } from '@angular/core/testing';

import { ProfileService } from './profile.service';
import { RequestService } from '../shared/request.service';

import { BrowserModule } from '@angular/platform-browser';
import { SharedModule } from '../shared/shared.module';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

describe('ProfileService', () => {

    const mockData = JSON.stringify([
        {
            fullName: 'John Doe',
            features: ['FEATURE 1', 'FEATURE 2']
        },
        {
            fullName: 'Jane Seinhorst',
            features: ['FEATURE 3', 'FEATURE 4']
        }
    ]);

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ProfileService,
                RequestService
            ],
            imports: [
                BrowserModule,
                SharedModule,
                FormsModule,
                HttpModule
            ]
        });
    });

    it('should be created', inject([ProfileService], (service: ProfileService) => {
        expect(service).toBeTruthy();
    }));

    it('should define public methods', inject([ProfileService], (service: ProfileService) => {
        expect(service.getFullName).toBeTruthy();
        expect(service.hasFeature).toBeTruthy();
        expect(service.getSession).toBeTruthy();
    }));

    it('should load the user\'s profile and set the correct features',
        inject(
            [ProfileService, RequestService],
            (service: ProfileService, requestService: RequestService) => {

        spyOn(requestService, 'ProfilesUsingGET').and.returnValue(Promise.resolve(mockData));

        service.getSession()
            .then(() => {
                expect(service.hasFeature('feature 1')).toBe(true);
                expect(service.hasFeature('FEATURE 1')).toBe(true);
                expect(service.hasFeature('feature 2')).toBe(true);
                expect(service.hasFeature('feature 3')).toBe(true);
                expect(service.hasFeature('feature 4')).toBe(true);

                expect(service.hasFeature('feature 5')).toBe(false);
                expect(service.hasFeature('feature 0')).toBe(false);

                // second time its should be loaded from cache
                service.getSession()
                    .then(() => {
                        expect(service.hasFeature('feature 1')).toBe(true);
                        expect(service.hasFeature('FEATURE 1')).toBe(true);
                        expect(service.hasFeature('feature 2')).toBe(true);
                        expect(service.hasFeature('feature 3')).toBe(true);
                        expect(service.hasFeature('feature 4')).toBe(true);

                        expect(service.hasFeature('feature 5')).toBe(false);
                        expect(service.hasFeature('feature 0')).toBe(false);
                    });

            });

    }));

    it('should return the correct fullName',
        inject(
            [ProfileService, RequestService],
            (service: ProfileService, requestService: requestService) => {

        spyOn(requestService, 'ProfilesUsingGET').and.returnValue(Promise.resolve(mockData));

        service.getSession()
            .then(() => {
                expect(service.getFullName()).toBe('John Doe');
            });

    }));

});

1 个答案:

答案 0 :(得分:0)

https://github.com/angular/angular-cli/issues/5526所述,问题中描述的问题是Angular 4错误。升级到Angular 5后,问题就解决了。