我正在为我的自动生成器编写单元测试。 我注意到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();
});
});
});
谢谢, 利奥尔
答案 0 :(得分:0)
要模拟默认提示,您必须省略模拟对象中的变量。
var mockPrompts = {
libName: "test",
angularVersion: "1.2.7",
ngResourceRequired: false
};
这将触发" libVersion"
的默认值