Oto moja kontroler:Błąd: Oczekiwany szpiega, ale dostał funkcji
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();
}
}
Oto moja testów jednostkowych za to:
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.
});
});
}
Test wyrzuca błąd, gdy idzie do linii:
expect(controller.activate).toHaveBeenCalled();
mówiąc, że Exp objął szpiega, ale dostał funkcję. Aktywuj to funkcja, która jest wywoływana, gdy wywoływany jest konstruktor mojego kontrolera, który testuję. Czy ktoś może wskazać mi właściwy kierunek, proszę.
Próbowałem dodając linię
spyOn(controller, 'activate');
przed oczekiwać, otrzymuję następujący błąd.
Expected spy activate to have been called.
Error: Expected spy activate to have been called.
OK, więc teraz otrzymujesz oficjalny błąd testu. Czy funkcja "aktywuj" jest wywoływana podczas tworzenia kontrolera? – sma
tak, to się nazywa. – Aj1