Error: Expected a spy, but got Function

23,394

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.      
    });    
Share:
23,394
Aj1
Author by

Aj1

BY-Day: I work as .Net/UI Developer.

Updated on July 09, 2022

Comments

  • Aj1
    Aj1 almost 2 years

    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.

    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.
    
  • Aj1
    Aj1 over 8 years
    tried adding the spyon it throws this new errors Expected spy activate to have been called. Error: Expected spy activate to have been called.
  • Yaron Schwimmer
    Yaron Schwimmer over 8 years
    Maybe you should try to use and.callThrough when you spy on the function, like: spyOn(controller, 'activate').and.callThrough();