我们可以定义以下测试:
spyOn(x, 'funk').andReturn(true);
如果我们想要定义:
spyOn(x, 'funk').andReturn(false);
我们会得到一个错误,说funk已经被监视过了.
我们怎样才能有效地unspy使用新的返回值重新开始?
你可以重新训练间谍:
//--- CODE --------------------------
x = {
funk: function() {
return 1;
}
}
// --- SPECS -------------------------
describe('test x', function () {
it("trains spies", function () {
spyOn(x, 'funk');
x.funk.andReturn('a');
expect(x.funk()).toBe('a');
x.funk.andCallThrough();
expect(x.funk()).toBe(1);
});
});
Run Code Online (Sandbox Code Playgroud)
请看这里的小提琴 - http://jsfiddle.net/eitanp461/88kvnzx3/
| 归档时间: |
|
| 查看次数: |
1387 次 |
| 最近记录: |