angularjs Chai-spies:AssertionError:预期{ Spy }

hts6caw3  于 2023-09-30  发布在  Angular
关注(0)|答案(1)|浏览(680)

我使用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)

如果我显式调用函数,为什么会发生这种情况?谢谢

yduiuuwa

yduiuuwa1#

只是为了记录,我认为你理解错了the docs。用这个:

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

你只是将函数scope.selectedCompetitionChanged Package 在另一个函数spy中,所以如果你想看到调用的次数,你必须使用新的spy()而不是原来的scope.selectedCompetitionChanged()
跟踪对象方法的另一种方法如下:

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

现在您可以像往常一样调用scope.selectedCompetitionChanged(),它将被视为间谍调用。

相关问题