错误:期待间谍,但得到了功能

时间:2022-12-06 15:44:24

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

Here is my unit test for that:

这是我的单元测试:

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

});
}

The test throws error when it goes to the line:

当测试到达该行时,测试会抛出错误:

 expect(controller.activate).toHaveBeenCalled();

saying that Expected a spy, but got Function. Activate is a function that gets called when i call constructor of my controller which I am testing. Can some one point me in right direction please.

说,期待一个间谍,但得到了功能。当我调用我正在测试的控制器的构造函数时,Activate是一个被调用的函数。有人可以指出我正确的方向吗。

Tried adding the line

尝试添加线

    spyOn(controller, 'activate'); 

before expect, I am getting the following error.

在期待之前,我收到以下错误。

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

1 个解决方案

#1


12  

You need to spy on a function before you test whether or not its been called. Try this:

在测试是否调用函数之前,您需要监视函数。尝试这个:

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


12  

You need to spy on a function before you test whether or not its been called. Try this:

在测试是否调用函数之前,您需要监视函数。尝试这个:

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