我正在尝试为我的Yeoman生成器编写测试,该生成器将调用命令行实用程序以在我正在脚手架的文件夹中生成一些文件。我已经看到了各种示例,这些示例说明了如何设置超时以等待函数完成,但是正在努力使其在本地运行。
这是我的考试:
describe('Should properly scaffold with config for Spring and wsdl2rest', function () {
before(function () {
basicProps.name = 'MyAppMock';
basicProps.package = 'com.generator.mock';
basicProps.camelVersion = '2.18.2';
basicProps.camelDSL = 'spring';
var wsdlPath = path.join(__dirname, '../test/address.wsdl');
basicProps.wsdl = wsdlPath;
basicProps.outdirectory = 'src/main/java';
return helpers.run(path.join(__dirname, '../app'))
.inTmpDir(function (dir) {
var done = this.async(); // `this` is the RunContext object.
fs.copy(path.join(__dirname, '../templates'), dir, done);
basicProps.outdirectory = path.join(dir, 'src/main/java');
})
.withOptions({ wsdl2rest: true })
.withPrompts({ name: basicProps.name })
.withPrompts({ camelVersion: basicProps.camelVersion })
.withPrompts({ camelDSL: basicProps.camelDSL })
.withPrompts({ package: basicProps.package })
.withPrompts({ wsdl: basicProps.wsdl })
.withPrompts({ outdirectory: basicProps.outdirectory })
.toPromise();
});
it('Should create the basic structure two ways', function () {
assert.file('pom.xml');
assert.file('README.md');
assert.file('src/main/resources/META-INF/spring/camel-context.xml');
assert.file('src/main/resources/META-INF/spring/camel-context-rest.xml')
});
});
问题是命令行可执行文件在测试之后完成,以查看其生成的文件是否存在,所以我得到了:
Creating wsdl2rest java output directory
calling: java -jar C:\Users\brianf\Documents\GitHub\generator-camel-project-fork\app\wsdl2rest\target\wsdl2rest-impl-fatjar-0.1.3-SNAPSHOT.jar --wsdl file:///C:/Users/brianf/Documents/GitHub/generator-camel-project-fork/test/address.wsdl --out C:\Users\brianf\AppData\Local\Temp\8d84f15024327cbe792407e1294ab46a5b4a1080\src\main\java --camel-context C:\Users\brianf\AppData\Local\Temp\8d84f15024327cbe792407e1294ab46a5b4a1080\src\main\resources\META-INF\spring\camel-context-rest.xml
1) Should create the basic structure two ways
11 passing (411ms)
1 failing
1) generator-camel:wsdl2rest
Should properly scaffold with config for Spring and wsdl2rest
Should create the basic structure two ways:
AssertionError [ERR_ASSERTION]: src/main/resources/META-INF/spring/camel-context-rest.xml, no such file or directory
+ expected - actual
-false
+true
at convertArgs.forEach.file (node_modules\yeoman-assert\index.js:56:12)
at Array.forEach (<anonymous>)
at Function.assert.file (node_modules\yeoman-assert\index.js:54:26)
at Context.<anonymous> (test\app.js:206:14)
stdout: Retrieving document at 'file:/C:/Users/brianf/Documents/GitHub/generator-camel-project-fork/test/address.wsdl'.
stderr: log4j:WARN No appenders could be found for logger (org.jboss.fuse.wsdl2rest.impl.WSDLProcessorImpl).
stderr: log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
wsdl2rest generated artifacts successfully
让这个东西等待的秘密是什么?我很肯定我缺少明显的东西,但是我主要是Java程序员,而不是JavaScript,并且在语言的某些异步方面有所挣扎。
谢谢!
更新:尽管有人建议我使用Mocha的异步代码选项(https://mochajs.org/#asynchronous-code),但我很难将这些概念用于我编写的测试中,如果有人解决了这个问题,可以使用一些其他帮助用Yeoman发电机测试?
答案 0 :(得分:1)
在此处查看Mocha文档-https://mochajs.org/#asynchronous-hooks
似乎您可能需要将“之前”放置在“描述”之外。或者,您可以将“ it”包装在另一个“描述”中。
答案 1 :(得分:1)
感谢@Evan,我们昨天找到了解决方案...
有两部分-其中之一是我们创建的用来实际调用Java jar的方法没有返回Promise ...,因此我们将其更改为:
console.log('calling: ' + cmdString);
return new Promise((resolve, reject) => {
const wsdl2rest = exec(cmdString);
wsdl2rest.stdout.on('data', function (data) {
console.log(`stdout: ${data}`);
});
wsdl2rest.stderr.on('data', function (data) {
console.log(`stderr: ${data}`);
});
wsdl2rest.on('close', function (code) {
if (code === 0) {
console.log(`wsdl2rest generated artifacts successfully`);
resolve()
} else {
reject()
console.log(`stderr: ${code}`);
console.log(`wsdl2rest did not generate artifacts successfully - please check the log file for details`);
}
});
})
我们将测试更改为:
describe('generator-camel:wsdl2rest', function () {
describe('Should properly scaffold with config for Spring and wsdl2rest', function () {
it('Should create the basic structure two ways', function () {
basicProps.name = 'MyAppMock';
basicProps.package = 'com.generator.mock';
basicProps.camelVersion = '2.18.2';
basicProps.camelDSL = 'spring';
var wsdlPath = path.join(__dirname, '../test/address.wsdl');
basicProps.wsdl = wsdlPath;
basicProps.outdirectory = 'src/main/java';
return helpers.run(path.join(__dirname, '../app'))
.inTmpDir(function (dir) {
var done = this.async(); // `this` is the RunContext object.
fs.copy(path.join(__dirname, '../templates'), dir, done);
basicProps.outdirectory = path.join(dir, 'src/main/java');
})
.withOptions({ wsdl2rest: true })
.withPrompts({ name: basicProps.name })
.withPrompts({ camelVersion: basicProps.camelVersion })
.withPrompts({ camelDSL: basicProps.camelDSL })
.withPrompts({ package: basicProps.package })
.withPrompts({ wsdl: basicProps.wsdl })
.withPrompts({ outdirectory: basicProps.outdirectory })
.toPromise()
.then(() => {
assert.file('pom.xml');
assert.file('README.md');
assert.file('src/main/resources/META-INF/spring/camel-context.xml');
assert.file('src/main/resources/META-INF/spring/camel-context-rest.xml')
});
});
});
所有这些都准备就绪,我们就可以运行toPromise并等待执行断言。
感谢您对Evan的指导!
答案 2 :(得分:0)
@tbking提供了正确的方法。效果很好-感谢您的有用提示!
it('Should create the basic structure two ways', function () {
basicProps.name = 'MyAppMock';
basicProps.package = 'com.generator.mock';
basicProps.camelVersion = '2.18.2';
basicProps.camelDSL = 'spring';
var wsdlPath = path.join(__dirname, '../test/address.wsdl');
basicProps.wsdl = wsdlPath;
basicProps.outdirectory = 'src/main/java';
return helpers.run(path.join(__dirname, '../app'))
.inTmpDir(function (dir) {
var done = this.async(); // `this` is the RunContext object.
fs.copy(path.join(__dirname, '../templates'), dir, done);
basicProps.outdirectory = path.join(dir, 'src/main/java');
})
.withOptions({ wsdl2rest: true })
.withPrompts({ name: basicProps.name })
.withPrompts({ camelVersion: basicProps.camelVersion })
.withPrompts({ camelDSL: basicProps.camelDSL })
.withPrompts({ package: basicProps.package })
.withPrompts({ wsdl: basicProps.wsdl })
.withPrompts({ outdirectory: basicProps.outdirectory })
.toPromise().then(function(value) {
it('Should create the basic structure two ways', function () {
console.log('testing...');
assert.file('pom.xml');
assert.file('README.md');
assert.file('src/main/resources/META-INF/spring/camel-context.xml');
assert.file('src/main/resources/META-INF/spring/camel-context-rest.xml')
});
});
});