使用composeWith进行自动子发电机测试

时间:2015-09-29 12:43:15

标签: javascript node.js yeoman-generator yo

我有一个带有自己的本地子生成器应用程序的生成器 - 生成器应用程序。情况是我希望子运行器在运行主“app”生成器时使用预定义的参数调用至少一次。除了npm测试失败之外,一切正常。

主'应用'发生器:哟mygen。
子发电机:哟mygen:foo“lorem ipsum”。

这是test.js

/*global describe, beforeEach, it*/
'use strict';

var path = require('path'),
yg = require('yeoman-generator');
var helpers = require('yeoman-generator').test;
var assert = require('yeoman-assert');

describe(' running `yo mygen`', function () {
  before(function (done) {
    var deps = [
      [helpers.createDummyGenerator(), 'mygen:foo', 'blah blah blah']
    ];

    helpers.run(path.join(__dirname, '../app'))
      .inDir(path.join(__dirname, './temp'))  // Clear the directory and set it as the CWD
      .withOptions({ mongoose: 'app', 'skip-install': true })            // Mock options passed in
      .withPrompts({
        'dbName': 'demo',
        'useUserAuth': false
      })
      .withGenerators(deps)
      .on('end', done);
    //done();
  });

  it('can be imported without blowing up', function () {
    var app = require('../app');
    assert(app !== undefined);
  });

  it('creates all required MVC files', function (done) {
    var expected = [
      // add files you expect to exist here.
      'package.json',
      'app.js',
      'bower.json',
      'routes/index.js',
      'public/css/style.css',
      'public/js/script.js',
      'views/index.html',
      'README.md',
      '.editorconfig',
      '.jshintrc'
    ];

    assert.file(expected);
    done();
  });
});

describe('í ¼ running `yo mygen:foo`', function () {
  before(function (done) {
    helpers.run(path.join(__dirname, '../schema'))
      .inDir(path.join(__dirname, './temp'))  // Clear the directory and set it as the CWD
      .withOptions({ mongoose: 'schema' })            // Mock options passed in
      .withArguments(['ha ha ha ha hha'])
      .on('end', done);
    //done();
  });

  describe('foo generator', function () {
    it('foo can be imported without blowing up', function () {
      var app = require('../foo');
      assert(app !== undefined);
    });

    it('created new MVC files for foo', function (done) {
      var expected = [
        // add files you expect to exist here.
        'view/t1.js',
        'models/t1.js',
        'controller/t1.js'
      ];
      assert.file(expected);
      done();
    })
  });
});

在app / index.js中,为了调用子生成器,我使用了:

mygenGenerator.prototype.install = function install(){
  this.installDependencies();
  this.composeWith("mygen:foo", {args: ["humpty dumpty saton a wall"]});
};

在stackoverflow和其他地方搜索了所有可能的答案。无法弄清楚该怎么做。

npm测试的测试用例失败:

  1. 从index.js中删除composeWith行,测试通过。
  2. 保持composeWith线,测试进入无限,最终超过2000ms配额并失败。

0 个答案:

没有答案