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.
OK, quindi stai ottenendo un errore di prova ufficiale ora. La funzione 'activate' viene chiamata durante l'istanziazione del controller? – sma
sì viene chiamato. – Aj1