gpt4 book ai didi

angularjs - createpy 和createspyobj 有什么区别

转载 作者:行者123 更新时间:2023-12-03 07:08:54 29 4
gpt4 key购买 nike

我在我的代码中使用过类似的内容。

return $provide.decorator('aservice', function($delegate) {
$delegate.addFn = jasmine.createSpy().andReturn(true);
return $delegate;
});

createSpy 是做什么的?我可以将 createSpy 调用更改为createspyobj 调用吗?

通过使用createSpy,我们可以创建一个函数/方法模拟。 Createspyobj 可以进行多个函数模拟。我说得对吗?

有什么区别。

最佳答案

当没有要监视的函数时,可以使用

jasmine.createSpy。它将像 spyOn 一样跟踪调用和参数,但没有实现。

jasmine.createSpyObj 用于创建一个模拟来监视一个或多个方法。它返回一个对象,该对象具有每个 spy 字符串的属性。

如果你想创建一个模拟,你应该使用jasmine.createSpyObj。查看下面的示例。

来自 Jasmine 文档 http://jasmine.github.io/2.0/introduction.html ...

创建 spy :

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");
});
});

创建SpyObj:

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);
});
});

关于angularjs - createpy 和createspyobj 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321307/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com