programing

createspy와 createspyobj의 차이점은 무엇입니까?

lastcode 2023. 3. 8. 21:14
반응형

createspy와 createspyobj의 차이점은 무엇입니까?

나는 내 코드에 다음과 같은 것을 사용했다.

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

createSpy는 어떻게 합니까?createSpy 콜을 createpyobj 콜로 변경할 수 있습니까?

createSpy를 사용하면 함수/메서드 모크를 1개 만들 수 있습니다.createspyobj는 여러 기능 모크를 수행할 수 있습니다.내 말이 맞니?

뭐가 다를까?

jasmine.createSpy감시 기능이 없을 때 사용할 수 있습니다.콜과 인수를 추적합니다.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);
  });
});

언급URL : https://stackoverflow.com/questions/24321307/what-is-the-difference-between-createspy-and-createspyobj

반응형