如何多次断言存根提取

How to assert stubbed fetch more than once

使用 proxyquire、sinon 和 mocha。

我能够在第一次调用 fetch 时存根 fetch。但是在递归的第二个获取调用中,我无法断言它。从输出来看,断言可能 运行 在测试完成之前。您将在断言后通过 second fetch 控制台看到这一点。

index.js

var fetch = require('node-fetch');

function a() {
  console.log('function a runs');  
  fetch('https://www.google.com')
    .then((e) => {
      console.log('first fetch');
      b();
    })
    .catch((e)=> {
      console.log('error')
    });
}

function b() {
  fetch('https://www.google.com')
    .then((e) => {
      console.log('second fetch');
    })
    .catch((e)=> {
      console.log('error')
    });
}
a()

测试:

describe('fetch test demo', ()=> {

  it('fetch should of called twice', (done)=> {

    fetchStub = sinon.stub();
    fetchStub2 = sinon.stub();
    fetch = sinon.stub();


    fetchStub.returns(Promise.resolve('hello'));
    fetchStub2.returns(Promise.resolve('hi'));

    var promises = [ fetchStub, fetchStub2 ]

    fetch.returns(Promise.all(promises));

    proxy('../index', {
        'node-fetch': fetch
      });

    fetch.should.have.been.callCount(2);
    done()
  });

});

   fetch test demo
function a runs
    1) fetch should of called twice
first fetch
second fetch

  lifx alert test
    - fetch should of called three times
    when rain change is over 50%
      - should run fetch twice


  0 passing (78ms)
  2 pending
  1 failing

  1) fetch test demo fetch should of called twice:
     expected stub to have been called exactly twice, but it was called once
    stub(https://www.google.com) => [Promise] {  } at a (/home/one/github/lifx-weather/foobar.js:5:3)
  AssertionError: expected stub to have been called exactly twice, but it was called once
      stub(https://www.google.com) => [Promise] {  } at a (foobar.js:5:3)
      at Context.it (test/bar.js:22:28)

更新版本

@dman,因为你更新了你的测试用例,我欠你一个更新的答案。虽然改写了,但场景仍然是非正统的 - 似乎您想在某种意义上忽略 'law of gravity',即使您知道它就在您面前。

我会尽量描述性强。你有两个函数,它们在设计上做着 async 的事情。 a() 按顺序调用 b() - 顺便说一下,这不是 recursion。这两个函数在完成/失败时都不会通知调用者,即它们被视为 fire-and-forget.

现在,让我们看看您的测试场景。您创建了 3 个存根。其中两个解析为一个字符串,一个使用 Promise.all() 组合它们的执行。接下来,您代理 'node-fetch' 模块

proxy('./updated', {
    'node-fetch': fetch
});

使用 returns 组合执行存根 1 和 2 的存根。现在,如果您在任一函数中打印出 fetch 的解析值,您将看到它不是字符串,而是存根数组。

function a () {
    console.log('function a runs');
    fetch('http://localhost')
        .then((e) => {
            console.log('first fetch', e);
            b();
        })
        .catch((e) => {
            console.log('error');
        });
}

我猜这不是预期的输出。但是让我们继续前进,因为无论如何这都不会终止您的测试。接下来,您将断言与 done() 语句一起添加。

fetch.should.have.been.callCount(2);
done();

这里的问题是无论你是否使用done(),效果都是一样的。您正在 sync 模式下执行场景。当然在这种情况下,断言总是会失败。但这里重要的是理解为什么

因此,让我们重写您的场景以模仿您要验证的行为的 异步 性质。

'use strict';

const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();

const proxy = require('proxyquire');

describe('fetch test demo', () => {

    it('fetch should of called twice', (done) => {

        var fetchStub = sinon.stub();
        var fetchStub2 = sinon.stub();
        var fetch = sinon.stub();

        fetchStub.returns(Promise.resolve('hello'));
        fetchStub2.returns(Promise.resolve('hi'));

        var promises = [fetchStub, fetchStub2];

        fetch.returns(Promise.all(promises));

        proxy('./updated', {
            'node-fetch': fetch
        });

        setTimeout(() => {
            fetch.should.have.been.callCount(2);
            done();
        }, 10);

    });

});

如您所见,所做的唯一更改是将断言包装在计时器块中。没什么 - 只需等待 10 毫秒,然后断言。现在测试按预期通过了。为什么?

嗯,对我来说这很简单。您想要测试 2 个顺序执行的 async 函数并且仍然 运行 您在 sync 模式下的断言。这听起来很酷,但它不会发生 :) 所以你有 2 个选择:

  • 让你的函数在完成时通知调用者,然后运行你的断言真正地异步模式
  • 使用非正统技术模仿事物的异步性质

根据原测试场景回复

It can be done. I've re-factored your provided files a bit so that can be executed.

index.js

const fetch = require('node-fetch');
const sendAlert = require('./alerts').sendAlert;

module.exports.init = function () {
  return new Promise((resolve, reject) => {

      fetch('https://localhost')
          .then(function () {
              sendAlert().then(() => {
                  resolve();
              }).catch(
                  e => reject(e)
              );
          })
          .catch(e => {
              reject(e);
          });

  });
};

alerts.js

const fetch = require('node-fetch');

module.exports.sendAlert = function () {
  return new Promise((resolve, reject) => {

      fetch('https://localhost')
          .then(function () {
              resolve();
          }).catch((e) => {
          reject(e);
      });

  });
};

test.js

'use strict';

const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();

const proxy = require('proxyquire');

describe.only('lifx alert test', () => {

  it('fetch should of called twice', (done) => {

      var body = {
          'hourly': {
              data: [{
                  time: 1493413200,
                  icon: 'clear-day',
                  precipIntensity: 0,
                  precipProbability: 0,
                  ozone: 297.17
              }]
          }
      };

      var response = {
          json: () => {
              return body;
          }
      };

      const fetchStub = sinon.stub();

      fetchStub.returns(Promise.resolve(response));
      fetchStub['@global'] = true;

      var stubs = {
          'node-fetch': fetchStub
      };

      const p1 = proxy('./index', stubs);

      p1.init().then(() => {

          try {
              fetchStub.should.have.been.calledTwice;
              done();
          } catch (e) {
              done(e);
          }
      }).catch((e) => done(e));

  });

});

What you're trying to do though is a bit unorthodox when it comes to good unit testing practices. Although proxyquire supports this mode of stubbing through a feature called global overrides, it is explained here why should anyone think twice before going down this path.

In order to make your example pass the test, you just need to add an extra attribute to the Sinon stub called @global and set it to true. This flag overrides the require() caching mechanism and uses the provided stub no matter which module is called from.

So, although what you're asking can be done I will have to agree with the users that commented your question, that this should not be adopted as a proper way of structuring your tests.

这里还有一个替代方法,使用 Promise.all()

注意:如果使用 fetch 的 json 方法,这将不起作用,您需要在 resolve() 中传递数据以实现数据逻辑。它只会在解决时通过存根。但是,它会断言调用的次数。

describe('fetch test demo', () => {

  it('fetch should of called twice', () => {

    let fetchStub  = sinon.stub();
    let fetchStub2 = sinon.stub();
    let fetch      = sinon.stub();

    fetchStub.returns(Promise.resolve('hello'));
    fetchStub2.returns(Promise.resolve('hi'));

    var promises = [ fetchStub, fetchStub2 ]
    var promise  = Promise.all(promises);

    fetch.returns(promise);

    proxy('../foobar', { 'node-fetch': fetch });

    return promise.then(() => {
      fetch.should.have.callCount(2);
    });

  });

});

我找到了另一种方法来完成任务。 可能这对某人有用。

    describe('Parent', () => {
      let array: any = [];
      before(async () => {
        array = await someAsyncDataFetchFunction();
        asyncTests();
      });
      it('Dummy test to run before()',async () => {
        expect(0).to.equal(0); // You can use this test to getting confirm whether data fetch is completed or not.
      });
      function asyncTests() {
        array.forEach((currentValue: any) => {
            describe('Child', async () => {
                it('Test '+ currentValue ,() => {
                    expect(currentValue).to.equal(true);
                  })
              })
          });
       }
   });

这就是我如何实现对数组的每个元素的断言。 (正在异步获取数组数据)。