从未使用chai-as-promised与sinon和nodejs调用存根

时间:2018-11-22 10:58:46

标签: node.js sinon chai sinon-chai chai-as-promised

我的单元测试遇到问题,完全卡住了,代码很简单,请了解发生了什么,我的存根从未被调用,集合似乎是正确的,这里的代码是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" style="display: block;">
    <a href="#" class="post-carousel">
      <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" class="carousel-img">
      <div id="expandedImgCaption" class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </a>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <a href="#" class="post-carousel">
        <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="" class="carousel-img">
        <div class="img-caption">
          <h3>Still more than 2 Millions+ people using</h3>
        </div>
      </a>
    </div>
    <div class="gallery-column">
      <a href="#" class="post-carousel">
        <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="" class="carousel-img">
        <div class="img-caption">
          <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
        </div>
      </a>
    </div>
    <div class="gallery-column">
      <a href="#" class="post-carousel">
        <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="" class="carousel-img">
        <div class="img-caption">
          <h3>sit amet, consectetur adipiscing elit. Donec</h3>
        </div>
      </a>
    </div>
    <div class="gallery-column">
      <a href="#" class="post-carousel">
        <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="" class="carousel-img">
        <div class="img-caption">
          <h3>nsectetur adipiscing elit. Donec</h3>
        </div>
      </a>
    </div>

  </div>
</div>

想了解现在的情况,该呼叫是正确的imo,使用mocha 5.2运行

Helpers.js:函数描述如下:

let strategy = fixtures.load('strategy')
chai.use(chaiAsPromised)

describe.only('Spawn Order Job', () => {

  let getPositionsStub, createJobStub, daoStub,sandbox
  beforeEach(()=>{
    sandbox = sinon.createSandbox()
    daoStub = sandbox.stub(dao, 'updateActiveOrders').resolves(true) //async
    getPositionsStub = sandbox.stub(strategyModule, 'getPositions') //sync
    createJobStub = sandbox.stub(helpers, 'createJob') //sync
    createJobStub.returns(true)
    getPositionsStub.resolves([{fake:'t'}, {fake:'t'}])
  })

  afterEach(()=>{
    sandbox.restore()
  })
  //OK
  it('Should failed with no param, type error context', ()=> {
    const promise = spawnOrderJob()
    expect(promise).to.be.rejectedWith(TypeError)
  })


  //OK
  it('Should throw error timeout order', () => {
    getPositionsStub.resolves([{fake:'t'}, {fake:'t'}])
    strategy.lastDateOrder = new Date()
    const ctx = { state: {strategy, dashboard, position:null}}
    const action = {b: true, s: false}
    const promise = spawnOrderJob(action, ctx)
    expect(getPositionsStub.called).to.be.true
    expect(daoStub.called).to.be.false
    expect(createJobStub.called).to.be.false
    expect(promise).to.be.rejectedWith(ORDER_ERROR, 'Timeout between order not expired.')
  })

  //KO stub never called
  it.only('Should pass validation on buy', () => {
    strategy.lastDateOrder = 0
    const ctx = { state: {strategy, dashboard, position: null }}
    const action = {b: true, s: false}
    const promise = spawnOrderJob(action, ctx)
    expect(promise).to.be.fulfilled
    expect(getPositionsStub.called).to.be.true //ok
    expect(createJobStub.called).to.be.true //never callled ????
    expect(daoStub.called).to.be.true //never called ????
  })
})

DAO

async function spawnOrderJob(action, ctx) {
  try {
    const { strategy, dashboard, position } = ctx.state
    const {b, s} = action

    //check in case strategy context
    if (strategy) {
      //pass validation buy contnext
      if (b) {
        //this stub is working
        const positions = await strategyModule.getPositions(ctx)

        const { maxPosition } = strategy.allocatedBTC
        const { activeOrders, maxActiveOrders, timeBetweenOrder, lastDateOrder } = strategy

        debug('Active orders:', strategy.activeOrders)
        debug('Position:', positions.length)

        if (activeOrders >= maxActiveOrders)
          throw new ORDER_ERROR('Max active orders reach.')

        if (positions.length + activeOrders >= maxPosition)
          throw new ORDER_ERROR('Max positions reach.')

        if (!timeoutExpired(lastDateOrder, timeBetweenOrder))
          throw new ORDER_ERROR('Timeout between order not expired.')

        //increment active orders counter
        //stub fail, but not called at all
        await dao.updateActiveOrders(strategy, true)
      }

      //Sell context
      if (s) {
        if (!position)
          throw new ORDER_ERROR('No position to sell')
      }
    }
    //stub fail, but called internally
    return createJob(constants.DASHBOARD_CREATE_ORDER, {
      orderType: b ? 'BUY' : 'SELL',
      title: `Strategy create order ( ${ b ? 'BUY' : 'SELL'} )`,
      strategy,
      dashboard,
      position
    })
  } catch (e) {
    throw e
  }
}


function createJob(name, data){
  //shortcut queue.create (kue.js)
  return queue.c(name,data)
}

module.exports = {
  createJob,
  spawnOrderJob
}

0 个答案:

没有答案