茉莉花间谍示例如何工作
How jasmine spy example works
全部;
我刚开始学习 Jasmine(版本 2.0.3),当我进入 Spies 部分时,第一个示例让我感到困惑:
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 函数不影响 describe 块中定义的栏? Jasmine 间谍如何处理这个?
谢谢
因为你实际上并没有执行这些方法。
如果您希望此测试失败:
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
在这些调用之后:
foo.setBar(123);
foo.setBar(456, 'another param');
那你应该给你的间谍打电话and.callThrough
。
spyOn(foo, 'setBar').and.callThrough();
Spies: and.callThrough
By chaining the spy with and.callThrough, the spy will still track all
calls to it but in addition it will delegate to the actual
implementation.
关于你的问题,'how jasmine deals with this?'
从here可以看到一个基本的解释:
Mocks work by implementing the proxy pattern. When you create a mock
object, it creates a proxy object that takes the place of the real
object. We can then define what methods are called and their returned
values from within our test method. Mocks can then be utilized to
retrieve run-time statistics on the spied function such as:
How many times the spied function was called.
What was the value that the function returned to the caller.
How many parameters the function was called with.
如果你想要所有的实现细节,你可以查看 Jasmine 源代码,它是 Open Source :)
在此源文件中 CallTracker 您可以看到如何收集有关方法调用的数据。
关于 the proxy pattern 的更多信息。
全部;
我刚开始学习 Jasmine(版本 2.0.3),当我进入 Spies 部分时,第一个示例让我感到困惑:
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 函数不影响 describe 块中定义的栏? Jasmine 间谍如何处理这个?
谢谢
因为你实际上并没有执行这些方法。
如果您希望此测试失败:
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
在这些调用之后:
foo.setBar(123);
foo.setBar(456, 'another param');
那你应该给你的间谍打电话and.callThrough
。
spyOn(foo, 'setBar').and.callThrough();
Spies: and.callThrough
By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.
关于你的问题,'how jasmine deals with this?'
从here可以看到一个基本的解释:
Mocks work by implementing the proxy pattern. When you create a mock object, it creates a proxy object that takes the place of the real object. We can then define what methods are called and their returned values from within our test method. Mocks can then be utilized to retrieve run-time statistics on the spied function such as:
How many times the spied function was called. What was the value that the function returned to the caller. How many parameters the function was called with.
如果你想要所有的实现细节,你可以查看 Jasmine 源代码,它是 Open Source :)
在此源文件中 CallTracker 您可以看到如何收集有关方法调用的数据。
关于 the proxy pattern 的更多信息。