nodejs单元测试错误" sinon.restore不是函数"

时间:2018-04-29 22:05:07

标签: node.js mocha sinon spy

嘿那里我试图通过使用sinon来编写单元测试,但是,我无法重置我的存根的sinon改变行为。首先我有

TypeError: Attempted to wrap getLastData which is already wrapped

经过一些研究,我得到了我需要重置sinon但是我得到了这个sinon.restore不是函数错误。此外,QueryHelper.getLastData()只返回一个promise,它通过返回像lastData

这样的实体来解析
var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var ExchangeHandlerFactory = require('../handler.js');
var QueryHelper = require('../query-helper.js');

describe('BinanceHandler', function() {
    var binanceHandler;
    var config;

    before(function() {
        config = {
            exchange: 'binance',
            interval: '1h'
        };
        var ExchangeHandlerFactoryObj = proxyquire('../handler.js', 
                                            {"./config.js": config,
                                                "./query-helper.js": QueryHelper});
        binanceHandler = ExchangeHandlerFactoryObj.getExchangeHandler('binance', '1h')
    });

    afterEach(() => {
        QueryHelper.getLastData.restore();
    });

    describe('#buildGetMarketTickerUrl()', function() {
        it('binance handler should return correct buildGetMarketTickerUrl', async function() {
            var lastData = {
                            symbol: "BTC-VTC",
                            interval: "1h",
                            exchange: "binance",
                            dataSet: [
                                {
                                    "o" : 0.0006882,
                                    "h" : 0.00071,
                                    "l" : 0.0006882,
                                    "c" : 0.0007008,
                                    "bv" : 63.27676876,
                                    "t" : 1524963600000
                                }
                        ]};
            sinon.stub(QueryHelper, "getLastData")
                .withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                .resolves(lastData);
            var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
            assert.equal(marketUrl, 
                         "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h&startTime=1524963600000");
        });
    });
});

注意:我还在afterEach()块中尝试了QueryHelper.getLastData.restore()。但后来我得到QueryHelper.getLastData.restore不是函数错误。

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:2)

我通过首先在beforeEach()钩子中创建QueryHelper的存根来解决这个问题。这是我做的修复。

beforeEach(function() {
    queryHelperStub = sinon.stub(QueryHelper, "getLastData");
});

afterEach(() => {
    queryHelperStub.restore();
});

describe('#buildGetMarketTickerUrl()', function() {
    it('binance handler should return correct buildGetMarketTickerUrl', async function() {
        var lastData = {
                        symbol: "BTC-VTC",
                        interval: "1h",
                        exchange: "binance",
                        dataSet: [
                            {
                                "o" : 0.0006882,
                                "h" : 0.00071,
                                "l" : 0.0006882,
                                "c" : 0.0007008,
                                "bv" : 63.27676876,
                                "t" : 1524963600000
                            }
                    ]};
        queryHelperStub.withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                        .resolves(lastData);
        var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
        assert.equal(marketUrl, 
                     "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h&startTime=1524963600000");
    });
});

describe('#buildGetMarketTickerUrl()', function() {
    it('binance handler should return correct buildGetMarketTickerUrl with startTime and endTime params', async function() {
        queryHelperStub.withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                        .resolves(null);
        var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
        assert.equal(marketUrl,
                     "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h");
    });
});

答案 1 :(得分:0)

如果有人需要的话,解决方法是使用.withArgs(arg1, arg2)并使用.callsArgOn(index, context)和多个链接:

stub.callsArgOn(1, arg1).callsArgOn(2, arg2)
...
stub.restore();

问题已注册sinon github