Hey there I am trying to write unit tests by using sinon ,however, I am not able to reset sinon changing behaviour of my stub. First I was having
TypeError: Attempted to wrap getLastData which is already wrapped
After some research I got that I need to reset sinon but I am getting this sinon.restore is not a function error. Additionally, QueryHelper.getLastData() just returns a promise which resolves by returning an entity like 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");
});
});
});
NOTE: I also tried QueryHelper.getLastData.restore()
in afterEach() block. But then I am getting QueryHelper.getLastData.restore is not a function error.
Thanks a lot for your help.
I resolved this by first creating stub of QueryHelper in beforeEach() hook. Here is the fix I did.
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");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With