yeoman 的 withGenerators 测试助手有什么用?

What can yeoman's withGenerators test helper be used for?

最近我一直在为 yeoman 生成器创建大量测试,但我无法理解 withGenerators() helper method 的用途。

给出的例子:

var angular = new RunContext('../../app');
 angular.withGenerators([
   '../../common',
   '../../controller',
   '../../main',
   [helpers.createDummyGenerator(), 'testacular:app']
 ]);

函数本身:

RunContext.prototype.withGenerators = function (dependencies) {
  assert(_.isArray(dependencies), 'dependencies should be an array');
  this.dependencies = this.dependencies.concat(dependencies);
  return this;
};

我看到该函数向 运行 上下文对象添加了一个依赖项数组,数组中的每个项目都是一个依赖生成器的路径。

这些路径有什么用?

何时以及为何需要使用此方法?

在给出的示例中,调用 angular.withGenerators([...]) 与从命令行调用 yo angularyo angular:commonyo angular:controller 等相同,或者以某种方式调用在实际生成器中模拟或修改对 composeWith() 的调用?

测试中的 运行 withGenerators() 和从生成器本身调用 composeWith() 有什么区别?

这用于 composeWith()(或传统的 invoke()hookFor() 方法)。

您可以在这里查看相关代码:https://github.com/yeoman/yeoman-test/blob/master/lib/index.js#L173-L188

基本上在调用 composeWith() 时,它会使用您传递的虚拟生成器 mock,而不是环境将解析的那个。

目前一个棘手的部分是,如果你将 local 路径设置传递给 composeWith,它将忽略存根 - 你可以看到这里 https://github.com/yeoman/generator/issues/704 填充的错误(票证建议一些手动解决方法)