Fehler: Erwartet ein Spion, bekam aber Funktion

Hier ist mein controller:

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

Hier ist mein unit-test:

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

});
}

Den test wirft Fehler, wenn er geht an die Linie:

 expect(controller.activate).toHaveBeenCalled();

sagen, dass Voraussichtlich ein Spion, bekam aber Funktion. Aktivieren Sie eine Funktion, die aufgerufen wird, wenn ich rufe Konstruktor meiner Steuerung, die ich Teste. Kann jemand Punkt mich in richtige Richtung bitte.

Versucht, indem die Linie

    spyOn(controller, 'activate'); 

bevor Sie erwarten, ich erhalte die folgende Fehlermeldung.

   Expected spy activate to have been called.
   Error: Expected spy activate to have been called.
OK, du bist also der erste offizielle test Fehler jetzt. Ist die activate Funktion, die aufgerufen wird während der Instanziierung von dem controller?
ja, es wird genannt.

InformationsquelleAutor Aj1 | 2015-11-11

Schreibe einen Kommentar