Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在异步函数中封装Mocha/Chai测试?_Javascript_Mocha.js_Chai - Fatal编程技术网

Javascript 如何在异步函数中封装Mocha/Chai测试?

Javascript 如何在异步函数中封装Mocha/Chai测试?,javascript,mocha.js,chai,Javascript,Mocha.js,Chai,我正在开发一个框架,其中包含使用承诺异步加载的模块。这些模块包含我想要为其创建测试的方法(对于这个问题,可以假设这些方法是同步的) 目前,我的代码类似于以下代码: describe("StringHelper", function() { describe("convertToCamelCase()", function() { it("should convert snake-cased strings to camel-case", function(done) {

我正在开发一个框架,其中包含使用承诺异步加载的模块。这些模块包含我想要为其创建测试的方法(对于这个问题,可以假设这些方法是同步的)

目前,我的代码类似于以下代码:

describe("StringHelper", function() {
    describe("convertToCamelCase()", function() {
        it("should convert snake-cased strings to camel-case", function(done) {
            Am.Module.load("Util").then(function() {
                var StringHelper = Am.Module.get("Util").StringHelper;
                //Test here
                done();
            });
        });
    });

    describe("convertToSnakeCase()", function() {
        it("should convert camel-cased strings to snake case.", function(done) {
            Am.Module.load("Util").then(function() {
                var StringHelper = Am.Module.get("Util").StringHelper;
                //Another test here
                done();
            });
        });
    });
});
鉴于
Am.Module.load()
本质上是对RequireJS的调用,其包装方式是返回一个承诺,因此在开始时只应加载一次,我如何重写上述内容

我基本上希望有这样的东西:

Am.Module.load("Util").then(function() {
    var StringHelper = Am.Module.get("Util").StringHelper;

    describe("StringHelper", function() {
        describe("convertToCamelCase()", function() {
            it("should convert snake-cased strings to camel-case", function(done) {
                //Test here
                done();
            });
        });

        describe("convertToSnakeCase()", function() {
            it("should convert camel-cased strings to snake case.", function(done) {
                //Another test here
                done();
            });
        });
    });
});

不幸的是,上述方法不起作用——测试根本没有被执行。报告器甚至不显示
描述(“StringHelper”)
的零件。有趣的是,在玩过之后,只有当所有测试都是以这种(第二个代码片段)方式编写时才会发生这种情况。只要至少有一个测试以第一种格式编写,测试就会正确显示。

您可以使用Mocha的
before()
钩子异步加载
Util
模块

describe("StringHelper", function() {
  var StringHandler;

  before(function(done) {
    Am.Module.load("Util").then(function() {
      StringHelper = Am.Module.get("Util").StringHelper;
      done();
    });
  });
  describe("convertToCamelCase()", function() {
    it("should convert snake-cased strings to camel-case", function() {
      //Test here
    });
  });

  describe("convertToSnakeCase()", function() {
    it("should convert camel-cased strings to snake case.", function() {
      //Another test here
    });
  });
});

我也有同样的问题。没想到我在多久前就发布了这个,但非常感谢!我喜欢挖掘未回答的问题,以防答案仍然有用。:-)干杯