【发布时间】:2015-02-12 05:57:42
【问题描述】:
我目前正在使用sinon.js 进行存根。 可以对方法进行存根和监视,但不能使用我所看到的库中的属性。
在下面的示例中,是否有办法检查 state.searchText 是否设置为 state.suggestion 在 submit 被调用之前?
// currently using angular but applies equally to vanilla js
$scope.searchSuggestion = function() {
this.state.searchText = this.state.suggestion;
this.submit();
};
理想的测试代码:
it('should set the searchText to be the suggestion', function(){
// arrange
sinon.stub(scope, 'submit');
scope.state.searchText = 'old value';
scope.state.suggestion = 'new value';
// act
scope.searchSuggestion();
// assert
expect(scope.submit.called).to.be(true)
// ~not sure how to see if searchText was set first
});
【问题讨论】:
-
不确定为什么要将
sinon与angularjs一起使用。 AngularJS 已经成为一个可测试的框架,它使单元测试变得非常容易。在您的情况下,或者在您调用searchSuggestion()之后添加expect(scope.state.searchText).toBe(scope.state.suggestion)行,以便您可以查看searchText是否具有新值。希望对您有所帮助。 -
是的,检查它是否已经改变不是问题。我想测试它在 before
submit被调用之前发生了变化。 Sinon.js 是一个存根框架。将它与 AngularJS 结合使用非常有用。在这种情况下使用它可以检查submit是否被称为expect(scope.submit.called).to.be(true)
标签: javascript angularjs sinon