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 回答
0

我有一个我用来做这个的助手,也许它对其他人有用。

// testUtils.js

module.exports = {
    spyOn: customSpyOn
};

function customSpyOn(obj, name) {
    const fn = obj[name];
    return {
        and: {
            wrapWith: wrapWith
        }
    };

    function wrapWith(wrapper) {
        obj[name] = jasmine.createSpy(`wrapped: ${name}`)
            .and.callFake(spyWrapper);
        return obj[name];

        function spyWrapper() {
            const args = [].slice.call(arguments);
            return wrapper.apply(obj, [fn].concat(args));
        }
    }
}

用法

如果您想在返回值中添加间谍;

const connection = socket.createConnection('ws://whatever.com/live', {
    format: 'json'
});

connection.open();
// etc...

您的规格和设置的顶部可能看起来像这样

// some.spec.js

const testUtils = require('./testUtils');

testUtils.spyOn(socket, 'createConnection')
    .and
    .wrapWith(function(original, url, options) {
        var api = original(url, options);
        spyOn(api, 'open').and.callThrough();
        spyOn(api, 'close').and.callThrough();
        spyOn(api, 'send').and.callThrough();
        return api;
    });
于 2016-01-22T12:04:41.163 回答