question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Karma, Jasmine, Uncaught Error: <toHaveBeenCalledTimes> : Expected a spy, but got Function without test

See original GitHub issue

Sorry in advance for my bad english…

I am testing my application with Karma and Jasmine and I have a really strange error.

I have 9 tests and when I run tests it seems that karma finds a 10th test. And Karma returns me a Jamsine error : Uncaught Error: : Expected a spy, but got Function

That is strange is : when I launch my test in debug mode. All is good. More strange is that I have 3 files. Error occured just when I execute 3 files at the same times. With 2 files all is good ^^ …

Have you already seen this error?

I will give you two screens. First no debug mode. Second debug mode. And I will give you my code.

Two screens of my IDE (No Debug and Debug)

hlxbh jw3ep

activate.controller.js ` describe(‘userActivateCtrl’, function() { beforeEach(module(‘Betizy’));

var $controller;

 beforeEach(inject(function(_$controller_, _ngToast_, _$filter_, _userService_, _navigationService_){
    $controller = _$controller_;
    ngToast = _ngToast_;
    $filter = _$filter_;
    userService = _userService_;
    navigationService = _navigationService_;
}));

describe('at its launch', function () {
    it('add an info toast in the toastList to say at the user what to do.', function () {
        var controller = $controller('userActivateCtrl', {
        });

        expect(ngToast.messages.length).toBe(1);
        expect(ngToast.messages[0].className).toBe('info');
        expect(ngToast.messages[0].content).toBe($filter('translate')('user.activate.message'));
    })
});

describe('for a valid registration', function () {
    it("call method from userService when activation is called by the click on activation button.", function () {
        var fakeHttpPromise = {
            then: function () {}
        };
        var controller = $controller('userActivateCtrl', {
    });
        controller.userActivateForm = {$valid: true};
        spyOn(userService, 'activate').and.returnValue(fakeHttpPromise);
        spyOn(navigationService, 'toIndex').and.returnValue(fakeHttpPromise);

        controller.activate();

    expect(userService.activate).toHaveBeenCalledTimes(1);

    setTimeout(function () {
        expect(navigationService.toIndex).toHaveBeenCalledTimes(1);
        done();
    })
})
});

describe('for an invalid activation form', function () {
it("does nothing.", function () {
    var controller = $controller('userActivateCtrl', {
    });
    var fakeHttpPromise = {
        then: function () {}
    };
    controller.userActivateForm = {$valid: false};
    spyOn(userService, 'activate').and.returnValue(fakeHttpPromise);
    ngToast.dismiss();
    controller.activate();
    expect(ngToast.messages.length).toBe(0);
    expect(userService.activate).not.toHaveBeenCalled();
})
});
});`

login.controller.js ` describe(‘userLoginCtrl’, function() {

beforeEach(module('Betizy'));



beforeEach(inject(function (_$controller_, _$stateParams_, _ngToast_, _$filter_) {
$controller = _$controller_;
$stateParams = _$stateParams_;
ngToast = _ngToast_;
$filter = _$filter_;
}));

describe('at its launch', function () {
it('add an info toast in the toastList to say at the user what to do.', function () {
    var controller = $controller('userLoginCtrl', {});

    expect(ngToast.messages.length).toBe(1);
    expect(ngToast.messages[0].className).toBe('info');
    expect(ngToast.messages[0].content).toBe($filter('translate')('user.login.message'));
});

it('add an danger toast in the toastList if user was mistaken during authentication.', function () {
    $stateParams.state = "error";

    var controller = $controller('userLoginCtrl', {});
    expect(ngToast.messages.length).toBe(2);
    expect(ngToast.messages[0].className).toBe('danger');
    expect(ngToast.messages[0].content).toBe($filter('translate')('user.login.error'));
});
});
});`

register.controller.js ` describe(‘userRegisterCtrl’, function(){ beforeEach(module(‘Betizy’));

var $controller;

beforeEach(inject(function(_$controller_, _userService_, _ngToast_, _$filter_, _navigationService_){
$controller = _$controller_;
userService = _userService_;
ngToast = _ngToast_;
$filter = _$filter_;
navigationService = _navigationService_;
}));

describe('at its launch', function () {
   it('add an info toast in the toastList to say at the user what to do.', function () {
   var controller = $controller('userRegisterCtrl', {
   });

   expect(ngToast.messages.length).toBe(1);
   expect(ngToast.messages[0].className).toBe('info');
   expect(ngToast.messages[0].content).toBe($filter('translate')('user.register.message'));

}) });

describe('for a valid registration', function () {
it("call method from userService when register is called by the click on register button.", function () {
    var fakeHttpPromise = {
        then: function () {}
    };
    var controller = $controller('userRegisterCtrl', {
    });
    controller.userRegisterForm = {$valid: true};
    spyOn(userService, 'register').and.returnValue(fakeHttpPromise);
    spyOn(navigationService, 'toIndex').and.returnValue(fakeHttpPromise);

    controller.register();

    expect(userService.register).toHaveBeenCalledTimes(1);

    setTimeout(function () {
        expect(navigationService.toIndex).toHaveBeenCalledTimes(1);
        done();
    })
})
});

describe('for an invalid registration with invalid mail adress', function () {
it("add a danger toast in the toastList for an incorrect mail adress pattern.", function () {
    var controller = $controller('userRegisterCtrl', {
    });
    controller.userRegisterForm = {$error: {email: true}, $valid: false};
    ngToast.dismiss();
    controller.register();
    expect(ngToast.messages.length).toBe(1);
    expect(ngToast.messages[0].className).toBe('danger');
    expect(ngToast.messages[0].content).toBe($filter('translate')('user.register.error.email.pattern'));
})
});

describe('for an invalid registration and a valid mail adress', function () {
it("does nothing.", function () {
    var controller = $controller('userRegisterCtrl', {
    });
    var fakeHttpPromise = {
        then: function () {}
    };
    controller.userRegisterForm = {$error: {email: false}, $valid: false};
    spyOn(userService, 'register').and.returnValue(fakeHttpPromise);
    ngToast.dismiss();
    controller.register();
    expect(ngToast.messages.length).toBe(0);
    expect(userService.register).not.toHaveBeenCalled();
})
});
});`

Thank you in advance ! 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
maksimrcommented, Feb 8, 2017

@SegmentationPro @peteringram0 It will be great if you create a test project with this problem.

Because It looks like a problem of test code, not karma, karma-jasmine or jasmine-core

Thanks

0reactions
peteringram0commented, Feb 8, 2017

I have stated getting this exact problem. My tests complete then at the end i get.

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  Error: <toHaveBeenCalledWith> : Expected a spy, but got Function.
  Usage: expect(<spyObj>).toHaveBeenCalledWith(...arguments)
  at jasmine-core/jasmine.js:3340

PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 47 of 78 ERROR (2.86 secs / 2.835 secs)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected a spy, but got Function - Stack Overflow
As you can see from my comment in (2) I get the message Error: Expected a spy, but got Function. The module works...
Read more >
spyOn report error: Expected a spy, but got Function #996
You'll see the error message that "Expected a spy, but got Function" when you use one of the matchers that expects to be...
Read more >
Jasmine – Test whether a method is called | A Developer's Blog
Language: Typescript (Angular Component Test) Method to be tested: Test: Error: : Expected a spy, but got Function. Solution: This was easy, ...
Read more >
Angularjs – Error: Expected a spy, but got Function - iTecNote
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 ...
Read more >
Reverse Engineering - understanding Spies in Testing
To test that it stores all invocation and their argument lets create another matcher function in our expect() method and call it toHaveBeenCalledWith()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found