0

出力を台無しにしたい関数があります。andCallThrough と andCallFake を組み合わせたようなものです。たとえば、次のコンストラクタ関数があるとします。

function Widget() {
  this.frobble = function() {return 1;};
}

function frotz() {
  return new Widget().frobble();
}

私ができるようにしたいのは、次のようなものです。

describe("Widget", function() {
  it("is created and frobbled when frotz() is called", function() {
    var widget;
    spyOn(window, 'Widget').andMessWithOutput(function(newWidget) {
      widget = newWidget;
      spyOn(widget, 'frobble').andCallThrough();
      frotz();
      expect(widget.frobble.calls.length).toBe(1);
    });
  });
});
4

2 に答える 2

0

これを行うために私が見つけた最良の方法は次のとおりです。

it("is clumsily created and frobbled when frotz() is called", function() {
  var widget;
  spyOn(window, 'Widget').andCallFake(function() {
    // originalValue is the original spied-upon value. note that if it's a constructor
    // you've got to call it with new (though that shouldn't seem unusual).
    widget = new Widget.originalValue(); 
    spyOn(widget, 'frobble').andCallThrough();
    frotz();
    expect(widget.frobble.calls.length).toBe(1);
  });
});
于 2012-12-12T03:24:38.307 に答える