开玩笑的测试失败了,但我得到的结果通过了测试

时间:2019-01-16 20:08:58

标签: javascript jestjs

我进行了以下测试:

// const

    const tickers =

 [{
      MarketName: 'USD-ETH',
      High: '155',
      Low: '98',
      Volume: 909770.82865232,
      Last: '100',
      BaseVolume: 0.47031163,
      TimeStamp: '2018-08-31T04:07:05.437',
      Bid: '100',
      Ask: '115',
      OpenBuyOrders: 81,
      OpenSellOrders: 478,
      PrevDay: '0.00000053',
      Created: '2016-05-16T06:44:15.287',
    },
    {
      MarketName: 'BTC-VTC',
      High: '0.00000039',
      Low: '0.00000035',
      Volume: 3208170.24052469,
      Last: '0.00000038',
      BaseVolume: 1.20114123,
      TimeStamp: '2018-08-31T04:04:34.45',
      Bid: '0.00000036',
      Ask: '0.00000038',
      OpenBuyOrders: 78,
      OpenSellOrders: 1038,
      PrevDay: '0.00000037',
      Created: '2014-10-31T01:43:25.743',
    },
    {
      MarketName: 'BTC-ADA',
      High: '0.00001506',
      Low: '0.00001413',
      Volume: 15638759.75591157,
      Last: '0.00001435',
      BaseVolume: 225.58363576,
      TimeStamp: '2018-08-31T04:07:06.467',
      Bid: '0.00001434',
      Ask: '0.00001438',
      OpenBuyOrders: 1755,
      OpenSellOrders: 7385,
      PrevDay: '0.00001503',
      Created: '2017-09-29T07:01:58.873',
    },
    {
      MarketName: 'USD-ADA',
      High: '0.04200000',
      Low: '0.04100000',
      Volume: 15638759.75591157,
      Last: '0.04200000',
      BaseVolume: 225.58363576,
      TimeStamp: '2018-08-31T04:07:06.467',
      Bid: '0.04100000',
      Ask: '0.04500000',
      OpenBuyOrders: 1755,
      OpenSellOrders: 7385,
      PrevDay: '0.03800000',
      Created: '2017-09-29T07:01:58.873',
    },
    {
      MarketName: 'USD-BTC',
      High: '3700',
      Low: '3400',
      Volume: 15638759.75591157,
      Last: '3500',
      BaseVolume: 225.58363576,
      TimeStamp: '2018-08-31T04:07:06.467',
      Bid: '3500',
      Ask: '3500',
      OpenBuyOrders: 1755,
      OpenSellOrders: 7385,
      PrevDay: '0.00001503',
      Created: '2017-09-29T07:01:58.873',
    }];
    // Mocks
mrBuyer.tickers = tickers;  


    mockBittrexInstance.createBuyOrder = jest.fn(() => ({ uuid: '614c34e4-8d71-11e3-94b5-425861b86ab6' }));

mrBuyer.orders = [{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'V',
  status: 'A',
  exchangeId: null,
  name: 'ADA',
  pair: 'USD',
  price: 0.04200000,
  quantity: 100,
  stopLoss: 0,
  stopLossPrice: 0.042,
  stopLossQuantity: 400.71693388,
  __v: 0,
},
{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'C',
  status: 'A',
  exchangeId: null,
  name: 'VTC',
  pair: 'BTC',
  price: 0.0000245,
  quantity: 25,
  stopLoss: 20,
  stopLossPrice: 0.00002328,
  stopLossQuantity: 0,
  __v: 0,
}];

Order.findOrders = jest.fn(() => ([{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'V',
  status: 'A',
  exchangeId: '725d45f5-8d71-11e3-94b5-425861b86ab6',
  name: 'ADA',
  pair: 'USD',
  price: 0.04200000,
  quantity: 100,
  stopLoss: 0,
  stopLossPrice: 0.042,
  stopLossQuantity: 400.71693388,
  __v: 0,
},
{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'C',
  status: 'A',
  exchangeId: '614c34e4-8d71-11e3-94b5-425861b86ab6',
  name: 'VTC',
  pair: 'BTC',
  price: 0.0000245,
  quantity: 25,
  stopLoss: 20,
  stopLossPrice: 0.00002328,
  stopLossQuantity: 0.0266737775,
  __v: 0,
}]));


test('it should create a buy order', async () => {
    mrBuyer.currentBalance = await jest.fn(() => ({ Balance: 0.10669511 }));
    await mrBuyer.getOrders();
    const buyOrders = mrBuyer.orders.filter(order => order.type === 'C' && order.status === 'A');
    expect(buyOrders[0].exchangeId).toBe('614c34e4-8d71-11e3-94b5-425861b86ab6');
  });

这是我的getOrders方法:

getOrders = async () => {
    const activeOrders = this.orders.filter(order => order.status === 'A');
    let creationCompleted = false;
    activeOrders.forEach(async (order) => {
      const currentTickerPrice = this.tickers.find(ticker => ticker.MarketName === `${order.pair}-${order.name}`);
      if (order.type === 'C' && !order.exchangeId && currentTickerPrice.Last <= order.price) {
        const buyOrderExist = this.buyOrders.find(buyOrder => buyOrder.id === order.id && buyOrder.price === order.price);
        if (!buyOrderExist) {
          this.buyOrders.push(order);
          creationCompleted = await this.createBuyOrder(order);
          if (creationCompleted) {
            console.log(this.orders);
            this.orders = await findOrders();
          }
        }
      }
    });
  }

这是我的createBuyOrder方法:

    createBuyOrder = async (order) => {
    try {
      let balanceBtc;
      // if pair is btc check if I have btc before create an order
      if (order.pair === 'BTC') {
        balanceBtc = await this.currentBalance('BTC');
        const currentBtcPrice = this.tickers.find(ticker => ticker.MarketName === 'USD-BTC');
        if (balanceBtc.Balance * currentBtcPrice.Last < 10) {
          return;
        }
      }

      if (order.pair === 'BTC' && order.stopLossQuantity === 0) {
        order.stopLossQuantity = +((balanceBtc.Balance * order.quantity) / 100); // eslint-disable-line
      }
      const buyParams = { market: `${order.pair}-${order.name}`, quantity: fixedWithoutRound(order.stopLossQuantity / order.price), rate: fixedWithoutRound(order.price) };
      const bittrexBuyOrder = await bittrex.createBuyOrder(buyParams);
      await findAndAddExchangeId(order.id, bittrexBuyOrder.uuid);
      order.exchangeId = bittrexBuyOrder.uuid; // eslint-disable-line
      console.log(order); // <<<<<<< HERE
      return true;  // eslint-disable-line
    } catch (error) {
      console.log(error);
    }
  }

在console.log中带有注释// // <<<<<<<在这里,我得到以下命令:

{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'V',
  status: 'A',
  exchangeId: '725d45f5-8d71-11e3-94b5-425861b86ab6',
  name: 'ADA',
  pair: 'USD',
  price: 0.04200000,
  quantity: 100,
  stopLoss: 0,
  stopLossPrice: 0.042,
  stopLossQuantity: 400.71693388,
  __v: 0,
},
{
  _id: '5b86e1f96811bc08e15ae682',
  type: 'C',
  status: 'A',
  exchangeId: '614c34e4-8d71-11e3-94b5-425861b86ab6',
  name: 'VTC',
  pair: 'BTC',
  price: 0.0000245,
  quantity: 25,
  stopLoss: 20,
  stopLossPrice: 0.00002328,
  stopLossQuantity: 0.0266737775,
  __v: 0,
}

并且您可以看到我有一个exchangeId,但是测试显示以下消息:

  ● Test MrBuyer › it should create a buy order

    expect(received).toBe(expected) // Object.is equality

    Expected: "614c34e4-8d71-11e3-94b5-425861b86ab6"
    Received: null

我不明白为什么我会得到null;

0 个答案:

没有答案