如何使用squire模拟内联requirejs依赖项以进行单元测试? [英] How to mock inline requirejs dependencies with squire for unit testing?

查看:89
本文介绍了如何使用squire模拟内联requirejs依赖项以进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我正在将requirejs与内联需求一起使用:

I'm using requirejs with inline requires, for instance:

define(['someDep'], function(someDep) {
  return {
    someFn: function() {
      require(['anotherDep'], function(anotherDep) {
        anotherDep.anotherFn();
      });
    }
  } 
});

在我的情况下,我不能在定义中包含anotherDep.

In my particular case, I cannot include anotherDep in the define.

在使用Mocha进行测试时,我有一个这样的测试用例:

When testing with mocha, I have a test case like this:

define(['squire'], function(Squire) {
  var squire = new Squire();
  describe('testcase', function() {
    it('should mock anotherDep', function(done) {
      var spy = sinon.spy();
      squire.mock('anotherDep', {
        anotherFn: spy
      });
      squire.require(['someDep'], function(someDep) {
        someDep.someFn();
        expect(spy).to.have.been.calledOnce;
        done();
      });
    });
  });
});

失败,因为anotherDep直接调用require而不是squire.require.解决方法是在全局范围内替换require

fails because anotherDep calls require directly and not squire.require. The work-around is to replace require in the global scope,

var originalRequire;

before(function() {
  originalRequire = require;
  require = _.bind(squire.require, squire);
});

after(function() {
  require = originalRequire;
});

这有效(请注意,必须以某种方式将squire.require绑定到squire对象,我正在使用下划线执行此操作),但是由于时间的限制,间谍仍不会被调用.测试也必须更改为

This works (note that squire.require must be bound to the squire object in some way, I'm using underscore to do this) except that the spy will still not be called because of timing. The test also has to change to

it('should mock anotherDep', function(done) {
  squire.mock('anotherDep', {
    anotherFn: function() {
      done();
    }
  });
  squire.require(['someDep'], function(someDep) {
    someDep.someFn();
  });
});

有更好的方法吗?如果不是这样,希望这可以为遇到相同问题的其他人提供解决方案.

Is there a better way? If not, hope this provides a solution for others running into the same problem.

推荐答案

我没有尝试专门做您要尝试做的事情,但在我看来,如果乡绅做得很彻底,则需要模块应该为您提供所需的内容,而不必弄乱全局require. require模块是一个特殊的(和保留的)模块,该模块提供了本地require功能.例如,当您使用Common JS语法糖时,这是必要的.但是,只要需要获取本地require,就可以使用它.同样,如果squire做得很彻底,那么它给您的require应该是squire的控制者,而不是某种原始的require.

I've not tried to do specifically what you are trying to do but it seems to me that if squire is doing a thorough job, then requiring the require module should give you what you want without having to mess with the global require. The require module is a special (and reserved) module that makes available a local require function. It is necessary for instance when you use the Common JS syntactic sugar. However, you can use it whenever you desire to get a local require. Again, if squire does a thorough job, then the require it gives you should be one that squire controls rather than some sort of pristine require.

所以:

define(['require', 'someDep'], function (require, someDep) {
  return {
    someFn: function() {
      require(['anotherDep'], function(anotherDep) {
        anotherDep.anotherFn();
      });
    }
  } 
});

这篇关于如何使用squire模拟内联requirejs依赖项以进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆