nodejs 单元测试错误 "sinon.restore is not a function"

nodejs unit test error "sinon.restore is not a function"

嘿,我正在尝试使用 sinon 编写单元测试,但是,我无法重置存根的 sinon 更改行为。首先我有

TypeError: Attempted to wrap getLastData which is already wrapped

经过一些研究,我发现我需要重置 sinon,但我得到的是 sinon.restore 不是函数错误。此外,QueryHelper.getLastData() 只是 returns 一个承诺,它通过返回一个像 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 is not a function error.

非常感谢您的帮助。

我通过首先在 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");
    });
});

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

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

问题已注册sinon github