createspy和createspyobj有什么区别 [英] What is the difference between createspy and createspyobj
问题描述
我在我的代码中使用过。
I have used in my code like.
return $provide.decorator('aservice', function($delegate) {
$delegate.addFn = jasmine.createSpy().andReturn(true);
return $delegate;
});
那是什么createSpy呢?我可以将createSpy调用更改为createspyobj调用。
In that what createSpy do? can i change the createSpy calls to createspyobj calls.
通过使用createSpy,我们可以创建一个函数/方法模拟。 Createspyobj可以做多个函数模拟。我是对的吗?
By using createSpy, we can create one function/method mocks. Createspyobj can do multiple functions mocks. Am i right?
有什么区别。
推荐答案
当没有窥探功能时,可以使用 jasmine.createSpy
。它将跟踪调用和参数,如 spyOn
,但没有实现。
jasmine.createSpy
can be used when there is no function to spy on. It will track calls and arguments like a spyOn
but there is no implementation.
jasmine .createSpyObj
用于创建将侦听一个或多个方法的模拟。它返回一个对象,该对象具有每个作为间谍的字符串的属性。
jasmine.createSpyObj
is used to create a mock that will spy on one or more methods. It returns an object that has a property for each string that is a spy.
如果要创建模拟,则应使用 jasmine.createSpyObj
。请查看以下示例。
If you want to create a mock you should use jasmine.createSpyObj
. Check out the examples below.
来自Jasmine文档 http://jasmine.github.io/2.0/introduction.html ...
From the Jasmine documentation http://jasmine.github.io/2.0/introduction.html...
createSpy:
createSpy:
describe("A spy, when created manually", function() {
var whatAmI;
beforeEach(function() {
whatAmI = jasmine.createSpy('whatAmI');
whatAmI("I", "am", "a", "spy");
});
it("is named, which helps in error reporting", function() {
expect(whatAmI.and.identity()).toEqual('whatAmI');
});
it("tracks that the spy was called", function() {
expect(whatAmI).toHaveBeenCalled();
});
it("tracks its number of calls", function() {
expect(whatAmI.calls.count()).toEqual(1);
});
it("tracks all the arguments of its calls", function() {
expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");
});
it("allows access to the most recent call", function() {
expect(whatAmI.calls.mostRecent().args[0]).toEqual("I");
});
});
createSpyObj:
createSpyObj:
describe("Multiple spies, when created manually", function() {
var tape;
beforeEach(function() {
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
tape.play();
tape.pause();
tape.rewind(0);
});
it("creates spies for each requested function", function() {
expect(tape.play).toBeDefined();
expect(tape.pause).toBeDefined();
expect(tape.stop).toBeDefined();
expect(tape.rewind).toBeDefined();
});
it("tracks that the spies were called", function() {
expect(tape.play).toHaveBeenCalled();
expect(tape.pause).toHaveBeenCalled();
expect(tape.rewind).toHaveBeenCalled();
expect(tape.stop).not.toHaveBeenCalled();
});
it("tracks all the arguments of its calls", function() {
expect(tape.rewind).toHaveBeenCalledWith(0);
});
});
这篇关于createspy和createspyobj有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!