我正在使用chai-spies来确保调用我的控制器中的函数,这是我的测试:
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();
});
其中scope.selectedCompetitionChanged是一个函数.测试失败,出现以下错误:
AssertionError: expected { Spy } to have been called
at Context.<anonymous> (base/tests/client/controllers/prediction.js?02f216981852d0775780926989e7266c6afb0af6:61:30)
如果我调用显式调用函数,怎么会发生这种情况?
谢谢
最佳答案 只是为了记录,我认为你理解错了
the docs.这个:
var spy = chai.spy(scope.selectedCompetitionChanged);
你只是将函数scope.selectedCompetitionChanged包装在另一个函数spy中,所以如果你想看到调用的数量,你必须使用新的spy()而不是原始的scope.selectedCompetitionChanged().
跟踪对象方法的另一种方法如下:
var spy = chai.spy.on(scope, 'selectedCompetitionChanged');
现在您可以像往常一样调用scope.selectedCompetitionChanged(),它将被视为间谍调用.