javascript - 如何多次断言 stub 提取

标签 javascript node.js unit-testing sinon proxyquire

使用 proxyquire、sinon 和 mocha。

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

索引.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,既然你更新了你的测试用例,我欠你一个更新的答案。尽管换了措辞,这个场景仍然是非正统的——在某种意义上,你似乎想忽略“万有引力定律”,即使你知道它就在你面前。

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

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

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

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

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);

    });

});

如您所见,所做的唯一更改是将断言包装在计时器 block 中。没什么 - 只需等待 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.

关于javascript - 如何多次断言 stub 提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690868/

相关文章:

javascript - 在 angularJS 中重置表单并关闭

javascript - 路由获取参数和应用逻辑

javascript - 点击 Node.js 站点时偶尔出现 ERR_CONNECTION_RESET 错误

node.js - Nodejs 请求模块失败并以验证码结束

xcode - 为什么右大括号显示没有代码覆盖?

python - 运行测试时, Nose 或 pytest 进入交互式控制台

javascript - 带有 ng-animate 的 ng-show 无法正常工作

javascript - 单击另一个 div 时打开 div

c# - 以编程方式测试 asp.net 页面

javascript - 在 FormPanel 中的特定字段集中动态添加组件