2015-11-11 9 views
5

Ecco il mio regolatore:Errore: si aspettava una spia, ma ha ottenuto Funzione

export class testController { 
    static $inject: string[] = ["testService", "$mdDialog", "$state"]; 
    constructor(public _testService: services.testService, private _mdDialog: any, private _$state: any) { 
     this.isCurrentlyEditing = false; 
     this.activate(); 
    } 
    } 

Ecco il mio unit test per questo:

import {test as service} from './test.service'; 
    import {test as ctrl} from './test.controller'; 

    export function main() { 
    describe("testController",() => { 

    var mdDialog: angular.material.IDialogService; 
    var state: ng.ui.IStateService; 
    var testService: any; 
    var controller: any; 

    beforeEach(function() { 
     angular.mock.module('comp.modules.addField');   
    }); 
    beforeEach(function() { 
     testService = { 
      showCLULayer:() => { } 
     }; 
    }); 

    beforeEach(module('comp')); 
    beforeEach(inject(($injector: any) => { 

     mdDialog = $injector.get('$mdDialog'); 
     state = $injector.get('$state'); 
     testService = $injector.get('testService'); 
     controller = new ctrl.testController(testService, mdDialog, state); 
    })); 

    it("should Create Controller",() => {   
     controller = new ctrl.testController(testService, mdDialog, state); 
     spyOn(testService, 'showCLULayer').and.callThrough(); 
     expect(controller).not.toBeNull(); 
     expect(controller.activate).toHaveBeenCalled(); 
     ////error Expected a spy, but got Function.  
    });  

}); 
} 

Il test genera un errore quando va alla linea:

expect(controller.activate).toHaveBeenCalled(); 

dicendo che Exp etto una spia, ma ha ottenuto la funzione. Attiva è una funzione che viene chiamata quando chiamo il costruttore del mio controller che sto testando. Qualcuno può indicarmi la giusta direzione per favore.

provato ad aggiungere la riga

spyOn(controller, 'activate'); 

prima si aspettano, sto ottenendo il seguente errore.

Expected spy activate to have been called. 
    Error: Expected spy activate to have been called. 
+0

OK, quindi stai ottenendo un errore di prova ufficiale ora. La funzione 'activate' viene chiamata durante l'istanziazione del controller? – sma

+0

sì viene chiamato. – Aj1

risposta

11

È necessario spiare una funzione prima di verificare se è stata chiamata o meno. Prova questo:

it("should Create Controller",() => {   
     controller = new ctrl.testController(testService, mdDialog, state); 
     spyOn(testService, 'showCLULayer').and.callThrough(); 
     expect(controller).not.toBeNull(); 

     spyOn(controller, 'activate'); // < ------------Spy on the function 
     expect(controller.activate).toHaveBeenCalled(); 
     ////error Expected a spy, but got Function.  
    });  
+1

provato ad aggiungere lo spyon getta questi nuovi errori Attesi spia attiva per essere stato chiamato. \t Errore: la spia prevista si attiva per essere stata chiamata. – Aj1

+0

Puoi pubblicare i nuovi errori nella tua domanda? – sma

+0

Sicuro. L'ho aggiornato. – Aj1

Problemi correlati