栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

createdpy和createspyobj有什么区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

createdpy和createspyobj有什么区别

jasmine.createSpy
没有任何功能可以监视时使用。它将像a那样跟踪调用和参数,
spyOn
但是没有实现。

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

如果要创建模拟,则应使用

jasmine.createSpyObj
。查看以下示例。

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

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:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616295.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号