1
全部;茉莉花间谍如何工作
我刚开始学习茉莉花(2.0.3版本),当我到间谍节,第一个例子让我感到困惑:
describe("A spy", function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, 'setBar');
foo.setBar(123);
foo.setBar(456, 'another param');
});
it("tracks that the spy was called", function() {
expect(foo.setBar).toHaveBeenCalled();
});
it("tracks all the arguments of its calls", function() {
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
});
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
});
我不知道是否有人能解释为什么setBar功能不影响块内定义的块吧?茉莉花间谍如何处理这件事?
谢谢
谢谢,最让我感兴趣的是茉莉花如何实现这一点,而无需运行实际功能并影响变量?基本上它是如何将所有东西都复制到其独立的范围内(如果我对此的理解是正确的)?你能给我更详细的介绍吗? – Kuan
让我更新一些更多的信息。 –
谢谢,我阅读http://www.tutorialspoint.com/design_pattern/proxy_pattern。htm根据我的理解,它基本上是使用一个内部对象来委托一个任务。但我有点想知道如何将我的例子与这种模式联系起来。 – Kuan