Unit testing Yeoman帮助程序提示默认值

Unit testing Yeoman帮助程序提示默认值,unit-testing,mocking,yeoman-generator,Unit Testing,Mocking,Yeoman Generator,我正在为我的yeoman生成器编写单元测试。 我注意到mockPrompt没有为我的提示值使用默认值。 例如,我有一个版本参数,默认值为0.0.1,但除非我在测试中为版本指定一个值,否则结果是一个空字符串。 这是故意的还是我做错了什么 这是index.js中的提示函数: var prompts = [{ name: 'libName', message: 'What do you want to call your lib?' },{ name: 'lib

我正在为我的yeoman生成器编写单元测试。 我注意到mockPrompt没有为我的提示值使用默认值。 例如,我有一个版本参数,默认值为0.0.1,但除非我在测试中为版本指定一个值,否则结果是一个空字符串。 这是故意的还是我做错了什么

这是index.js中的提示函数:

  var prompts = [{
    name: 'libName',
    message: 'What do you want to call your lib?'
  },{
        name: 'libVersion',
        message: 'What version would you like to set?',
        default: "0.0.1"
  },{
    name: 'libDesc',
    message: 'Describe your lib:'
  },{
    name: 'authorName',
    message: 'What is your full name?'
  },{
        name: 'angularVersion',
        message: 'Enter angular version:',
        default: '1.2.7'            
  },{
      name: 'ngResourceRequired',
      message: 'Do you require ngResource module?',
      type:'confirm',
      default: false
  }];

  this.prompt(prompts, function (props) {

    this.libName = props.libName;
    this.libVersion = props.libVersion;
    this.libDesc = props.libDesc;
    this.authorName = props.authorName;
    this.angularVersion = props.angularVersion;
    this.ngResourceRequired = props.ngResourceRequired;

    cb();
  }.bind(this));
这是我的测试代码:

describe('my-jslib:app', function () {
    var jslib;
    var appPath = 'customAppPath';
     var expected = [
        appPath + '/.htaccess',
        appPath + '/404.html',
        appPath + '/favicon.ico',
        appPath + '/robots.txt',
        appPath + '/styles/main.scss',
        appPath + '/views/main.html',
        appPath + '/index.html',
        '.bowerrc',
        '.editorconfig',
        '.gitignore',
        '.jshintrc',
        'Gruntfile.js',
        'package.json',
        'bower.json'
      ];
      var mockPrompts = {
                libName: "test",
                //libVersion: "0.0.1",
                angularVersion: "1.2.7",
                ngResourceRequired: false
              };  

      var genOptions = {
                'appPath': appPath,
                'skip-install': true,
                'skip-welcome-message': true,
                'skip-message': true
              };

      beforeEach(function (done) {
            helpers.testDirectory(path.join(__dirname, 'tmp'), function (err) {
              if (err) {
                done(err);
              }

              jslib = helpers.createGenerator(
                'my-jslib:app', [
                  '../../app', [
                              helpers.createDummyGenerator(), 'mocha:app'
                          ]
                ],
                false,
                genOptions
              );
              helpers.mockPrompt(jslib, mockPrompts);
              done();
            });
          });

      it('creates expected files', function (done) {
            var expected = [
              '.bowerrc',
              '.editorconfig',
              '.gitignore',
              '.gitattributes',
              '.jshintrc',
              'bower.json',
              'Gruntfile.js',
              'package.json',
              'README.md',
              'src/' + mockPrompts.libName + '.js',
              'test/spec/' + mockPrompts.libName + '.js',
              'test/.jshintrcs',
              'test/karma.conf.js'
            ];

            jslib.run({}, function () {
              helpers.assertFile(expected);
              helpers.assertFileContent('package.json',  new RegExp('"name": "' + mockPrompts.libName + '"'));
              helpers.assertFileContent('bower.json',  new RegExp('"name": "' + mockPrompts.libName + '"'));
              helpers.assertFileContent('bower.json', new RegExp('"angular": "' + mockPrompts.angularVersion + '"'));
              helpers.assertNoFileContent('bower.json', new RegExp('"angular-resource": "' + mockPrompts.angularVersion + '"'));

              done();
            });
          });

});
谢谢,
Lior

要模拟默认提示,必须省略模拟对象中的变量

var mockPrompts = {
    libName: "test",
    angularVersion: "1.2.7",
    ngResourceRequired: false
};
这将触发“libVersion”的默认值