带有es6-promisified对象的sinon stub

时间:2016-05-27 15:42:41

标签: javascript testing ecmascript-6 sinon es6-promise

好的我的设置如下: 使用节点6.2,es6-promisify,sinon,sinon-as-promised和babel来转发对es6导入/导出的支持。

我正在测试的代码看起来像这样:

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捣乱,这看起来很蹩脚。 E.g:

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

所以问题是,如果不完全明显,为什么我的原始代码不起作用?有没有办法让存根工作而不会每次都宣传原始的解决方案(例如,其他人如何让这种事情发挥作用)?

编辑:

当前代码如下所示:

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],
  ....

EDIT2:

而FWIW,babel生成的代码正在产生这个:

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

EDIT3:

好极了。我改变了我的来源,改为:

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

1 个答案:

答案 0 :(得分:2)

问题最终与ES6导入/导出的工作方式有关,具体而言,它们如何使代码看起来更好可防止轻松的间谍/存根。

采用此示例模块:

aimages

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

// 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()是本地(对模块)引用。 Sinon在导出的对象中存储对同一函数的另一个引用。

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

get

唉,唉,这是一个更丑陋的代码。