带有 es6-promified 对象的 sinon 存根

sinon stub with es6-promisified object

好的,我的设置如下: 使用 node 6.2、es6-promisify、sinon、sinon-as-promised 和 babel 转译对 es6 的支持 import/export.

我的测试代码如下所示:

const client = restify.createJsonClient({
    url: 'http://www.example.com'
});
export let get = promisify(client.get, {thisArg: client, multiArgs: true});

export default function* () {
    yield get('/some/path');
}

然后在我的测试文件中我有这样的东西:

import * as m from mymodule;
it('should fail', function(done) {
    let stub = sinon.stub(m, 'get').rejects('i failed');
    client.get('/endpoint/that/leads/to/mymodule/call', function(err, req, res, data) {
        stub.called.should.be.eql(true); // assertion fails!!
        done();
    }
});

我也试过打断原来的 client.get 调用,但这也不起作用。我唯一要做的就是在每次调用时即时承诺,并存根原始的 client.get,这看起来很蹩脚。例如:

export const client = restify.createJsonClient({
    url: 'http://www.example.com'
});
function get() {
    return promisify(client.get, {thisArg: client, multiArgs: true});
}

export default function* () {
    yield get('/some/path');
}

测试代码这样做:

import {module_client} from mymodule;
it('should fail', function(done) {
    let stub = sinon.stub(module_client, 'get').yields('i failed');
    client.get('/endpoint/that/leads/to/mymodule/call', function(err, req, res, data) {
        stub.called.should.be.eql(true); // assertion succeeds
        done();
    }
});

所以问题是,如果不是很明显的话,为什么我的原始代码不起作用?有没有一种方法可以使存根工作而无需每次都承诺原来的 restify(例如,其他人如何让这种事情工作)?

编辑:

当前代码如下所示:

const client = restify.createJsonClient({
    url: 'http://www.example.com'
});

export let get = promisify(client.get, {thisArg: client, multiArgs: true});

export default function*() {
    try {
        console.log(exports.get); // <= a large sinon stub object, I'll post that below
        yield exports.get(); // <= throws here, "exports.get is not a function"
    }
    catch(ex) {
        log.error('got an error', ex);
        throw ex;
    }
}

console.log 打印以下内容:

{ [Function: proxy]
  isSinonProxy: true,
  reset: [Function],
  invoke: [Function: invoke],
  named: [Function: named],
  getCall: [Function: getCall],
  getCalls: [Function],
  calledBefore: [Function: calledBefore],
  calledAfter: [Function: calledAfter],
  withArgs: [Function],
  matches: [Function],
  printf: [Function],
  calledOn: [Function],
  alwaysCalledOn: [Function],
  calledWith: [Function],
  calledWithMatch: [Function],
  alwaysCalledWith: [Function],
  ....

编辑 2:

FWIW,babel 生成的代码产生了这个:

let get = exports.get = (0, _es6Promisify2.default)(client.get, { thisArg: client, multiArgs: true });

编辑 3:

好吧超级奇怪。我更改了我的来源来执行此操作:

const client = restify.createJsonClient({
    url: 'http://www.example.com'
});

export let get = promisify(client.get, {thisArg: client, multiArgs: true});

export default function*() {
    try {
        let thePromise = exports.get(); // e.g. call exports.get on separate line from the yield
        yield thePromise; // and the throw now says 'undefined is not a function'. I should note that in both cases, the stack trace shows the error on node_modules/co/index.js at line 65.
    }
    catch(ex) {
        log.error('got an error', ex);
        throw ex;
    }
}

问题最终与 ES6 import/exports 的工作方式有关,特别是它们如何使您的代码看起来更好 防止容易 spying/stubbing。

以这个示例模块为例:

// my-module.js
function someFunction() {
  console.log('original');
};

export let get = someFunction;

export default function() {
  get();
};

该代码的测试用例可能如下所示:

import * as sinon from 'sinon';
import * as should from 'should';
import setup, * as myModule from './my-module';

it('should call get()', () => {
  let stub = sinon.stub(myModule, 'get');
  setup();
  stub.called.should.eql(true);
});

您会看到调用的是原始 get(),而不是存根。这是因为在模块中,get 是本地(对模块)引用。 Sinon 在导出的对象中存根 另一个 对同一函数的引用。

要完成此工作,您需要使用导出对象中的本地引用,而不是在模块中使用本地引用:

export default function() {
  exports.get();
};

唉,这使得代码更丑陋。