createpy 和 createpyobj 有什么区别 [英] What is the difference between createspy and createspyobj

查看:9
本文介绍了createpy 和 createpyobj 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用过类似的.

I have used in my code like.

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

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

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可以在没有spy函数的时候使用.它将像 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.

如果你想创建一个 mock,你应该使用 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...

创建间谍:

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

这篇关于createpy 和 createpyobj 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆