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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With