Chai-spies:AssertionError:期望{间谍}

时间:2015-09-28 16:24:53

标签: angularjs unit-testing chai

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

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

1 个答案:

答案 0 :(得分:3)

仅仅是为了记录,我认为你理解错误the docs。有了这个:

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

您只是将函数scope.selectedCompetitionChanged包含在另一个函数spy中,因此如果您想查看调用次数,则必须使用新的spy()而不是原始scope.selectedCompetitionChanged() 1}}。

跟踪对象方法的另一种方法如下:

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

现在你可以像往常一样打电话给scope.selectedCompetitionChanged(),这将被视为间谍电话。