【发布时间】:2017-07-11 20:14:05
【问题描述】:
我正在尝试用 JavaScript 编写测试,我正在测试的方法进行了 2 次方法调用(model.expandChildren() 和 view.update();)
// inside 'app' / 'RV.graph'
var expandChildren = function(){
return model.expandChildren().then(function(r) {
view.update(r);
});
};
我尝试使用 Jasmine 规范编写测试以监视视图和模型功能,但似乎在给定测试中您只能有 1 个间谍。看来我在这里遗漏了一些重要的东西,应该有一种方法可以使用间谍来模拟多个方法调用,因为我的函数需要同时进行这两个调用。
我希望我的规范能够按照下面的方式运行,但它目前只通过了第一个测试(第一个间谍按预期运行),第二个测试失败,因为 Jasmine 正在尝试运行实际功能,而不是间谍功能:
var model = GRAPH.model;
var view = GRAPH.view;
var app = RV.graph;
describe('#expandChildren', function() {
beforeEach(function() {
// first spy, all good
spyOn(model, 'expandChildren').and.callFake(function() {
return new Promise(function(resolve) {
resolve(testResponse);
});
});
// second spy doesn't work because Jasmine only allows 1
spyOn(view, 'update');
app.expandChildren();
});
// passing test
it('calls model.expandChildren', function() {
expect(model.expandChildren).toHaveBeenCalled();
});
// failing test that runs the REAL view.update method
it('calls view.update', function() {
expect(view.update).toHaveBeenCalled();
});
});
有没有办法用 Jasmine 做到这一点?
【问题讨论】:
标签: javascript unit-testing jasmine