Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai-spies: AssertionError: expected { Spy }

I am using chai-spies to make sure a function in my controller is invoked, here's my test:

it('Should show right season and analysts when competition has been selected', function (done) {
    scope.selectedCompetition = scope.competitions[3];
    var spy = chai.spy(scope.selectedCompetitionChanged);
    scope.selectedCompetitionChanged();
    expect(spy).to.have.been.called();
    done();
  });

where scope.selectedCompetitionChanged is a function. The test fails with the following error:

 AssertionError: expected { Spy } to have been called
        at Context.<anonymous> (base/tests/client/controllers/prediction.js?02f216981852d0775780926989e7266c6afb0af6:61:30)

How come this happen if I invoke explicitly call the function? Thanks

like image 957
Dario Avatar asked Sep 28 '15 16:09

Dario


1 Answers

Just for the record, I think you understood wrong the docs. With this:

var spy = chai.spy(scope.selectedCompetitionChanged);

You are just wrapping the function scope.selectedCompetitionChanged inside another function spy, so if you want to see the number of calls you have to use the new spy() instead of the original scope.selectedCompetitionChanged().

Another way to track an object method is as follows:

var spy = chai.spy.on(scope, 'selectedCompetitionChanged');

Now you can call scope.selectedCompetitionChanged() as usual and it will count as a spy call.

like image 155
Fran Dios Avatar answered Sep 19 '22 14:09

Fran Dios