2017-11-15 2 views
0

Ich habe festgestellt, dass meine Coverage von meinem Angular 4-Projekt nicht zu 100% auf Probleme mit dem Konstruktor zurückzuführen ist. Ich verwende das Standard-Winkel-Cli-Setup. Im Coverage-Bericht wird angezeigt, dass der Konstruktor insgesamt 19 Mal aufgerufen wird, aber immer noch als nicht getestet markiert ist. Daher werden nicht alle Zweige getestet.Angular 4 unitest coverage nicht 100% wegen contructor

Hat mein Code irgendwelche Probleme? Gibt es einen Fehler im Deckungsbericht?

Mein Code:

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 ''; 
    } 
} 

Der Unit-Test:

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'); 
      }); 

    })); 

}); 

Antwort