node.js - 如何构建等待进程首先完成的 Mocha 测试?

标签 node.js testing mocha.js yeoman

我正在尝试为我的 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 解决了这个问题,我可以使用一些额外的帮助发电机测试?

最佳答案

在这里查看 Mocha 文档 - https://mochajs.org/#asynchronous-hooks

似乎您可能需要将“之前”放在“描述”之外。或者,您可以将“it”包装在另一个“describe”中。

关于node.js - 如何构建等待进程首先完成的 Mocha 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105600/

相关文章:

node.js - webdriver-manager 更新引发错误 "Invalid or unsupported zip format. No END header found"

node.js - 如何在本地查询区 block 链比特币

javascript - 如何在node.js中导出多个mongoose模型模块

ruby-on-rails - ruby rails 2.3.8 : Been having trouble getting testing things to work

testing - 遗传编程成功解决了哪些问题?

wcf - 推荐用于测试 SOAP 和 REST 服务的测试框架?

javascript - Mongoose - 按填充对象的字段搜索对象

node.js - 使用 grunt-mocha-test 创建测试组

node.js - Mocha 忽略了一些测试,尽管它们应该运行

node.js - 如何使用 sinon stub 异步数据库调用