vendredi 18 novembre 2016

Angular 2 testing. A spy does not get called in ngOnInit

I am trying to call "test" function in AuthenticationService on the AppComponents initialisation. ngOnInit it self does get called, but "testSpy" does not. If I call "test" fuction explicitly in the tests, then the test passes. Is my service injection incorrect ?

AppComponent

import { Component,OnInit } from '@angular/core';
import {Title} from '@angular/platform-browser';
import {AuthenticationService} from '../../services/authenticationService/authentication.service';
import {Observable} from 'rxjs/observable';
import {BackendMockService} from '../mockbackendModule/backend.mock.service';

@Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.css']
})


export class AppComponent implements OnInit {
   title = 'app works!';
   userAuthStatus:boolean=false;

   constructor(private authenticationService:AuthenticationService,private backend:BackendMockService){
}


   ngOnInit():void{
      let ok = this.authenticationService.test();

   }
}

app.component.spec

import {ComponentFixture, TestBed,async,inject,fakeAsync,tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {DebugElement} from '@angular/core';
import {AppComponent} from './app.component';
import {AuthenticationService} from '../../services/authenticationService/authentication.service';
import {MockBackEndModule} from '../../modules/mockbackendModule/mockbackend.module';
import {BackendMockService} from '../../modules/mockbackendModule/backend.mock.service';

let component:AppComponent;
let fixture: ComponentFixture<AppComponent>;
let debugEl : DebugElement;
let element :HTMLElement;
let authService:AuthenticationService;
let backend:BackendMockService;
let testSpy : any;
let ngOnInitSpy:any;

describe('App Component ',()=>{
   beforeEach(async(()=>{
      TestBed.configureTestingModule({
          imports:[MockBackEndModule],
          declarations:[AppComponent],
          providers:[AuthenticationService]

      }).compileComponents();

     fixture = TestBed.createComponent(AppComponent);
     component = fixture.componentInstance;
     backend = fixture.debugElement.injector.get(BackendMockService);        
     authService = fixture.debugElement.injector.get(AuthenticationService);
     testSpy = spyOn(authService,"test").and.returnValue("ok");
     ngOnInitSpy = spyOn(component,"ngOnInit");
     fixture.detectChanges();

  }));

   describe('On initialisation (ngOnInit()).',()=>{
      it('should call ngOnInit() on initialisation of the app component. ',()=>{
         fixture.detectChanges();      
         expect(ngOnInitSpy).toHaveBeenCalled();

      });

      it('should call the test function.',()=>{
         fixture.detectChanges();
         expect(testSpy).toHaveBeenCalled();
      });

    });
 });

Authentication Service

import {Injectable,Inject} from '@angular/core';
import {AuthenticationMockUp} from './authentication.mockup';
import {Observable} from 'rxjs/observable';
import { Http, Response,Headers,RequestOptions } from '@angular/http';
import {Subject} from 'rxjs/Subject';
import 'rxjs/add/operator/map';

@Injectable()

export class AuthenticationService {
   userProperties : UserProperties;
   _http:Http;

    constructor(@Inject(Http) _http: Http){
        this.userProperties = new UserProperties();
        this._http = _http;
    }

    test():string{
        return "ok";
    }

}

Aucun commentaire:

Enregistrer un commentaire