【问题标题】:Mocha test case - are nested it( ) functions kosher?Mocha 测试用例 - 嵌套 it() 函数是否符合规定?
【发布时间】:2015-12-21 08:14:04
【问题描述】:

我有这种情况,我想我想在 Mocha 测试中嵌套 it() 测试用例。我确信这是错误的,我没有看到任何建议来做我正在做的事情,但我目前真的不知道更好的方法 -

基本上,我有一个“父”测试,在父测试中有一个 forEach 循环,其中包含所有“子”测试:

it('[test] enrichment', function (done) {

        var self = this;

        async.each(self.tests, function (json, cb) {

            //it('[test] ' + path.basename(json), function (done) {

                var jsonDataForEnrichment = require(json);
                jsonDataForEnrichment.customer.accountnum = "8497404620452729";
                jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";
                var options = {
                    url: self.serverURL + ':' + self.serverPort + '/event',
                    json: true,
                    body: jsonDataForEnrichment,
                    method: 'POST'
                };


               request(options,function (err, response, body) {
                    if (err) {
                        return cb(err);
                    }

                     assert.equal(response.statusCode, 201, "Error: Response Code");
                     cb(null);


                });

            //});

        }, function complete(err) {
            done(err)
        });

    });

如您所见,两条单独的行被注释掉了 - 我想将它们包含在内,以便我可以轻松查看每个单独测试的结果,但是我遇到了这种尴尬的情况,即在回调旁边触发测试的回调对于 async.each。

以前有没有人见过这种情况并知道一个好的解决方案,测试人员可以轻松地看到循环中每个测试的结果?

【问题讨论】:

    标签: javascript node.js asynchronous jasmine mocha.js


    【解决方案1】:

    不要嵌套it 调用。同步调用它们。

    嵌套的it 调用在 Mocha 中永远不会好。 it 调用也不是异步执行的。 (测试可以是异步的,但你不能调用it异步。)这是一个简单的测试:

    describe("level 1", function () {
        describe("first level 2", function () {
            it("foo", function () {
                console.log("foo");
                it("bar", function () {
                    console.log("bar");
                });
            });
    
            setTimeout(function () {
                it("created async", function () {
                    console.log("the asyncly created one");
                });
            }, 500);
        });
    
        describe("second level 2", function () {
            // Give time to the setTimeout above to trigger.
            it("delayed", function (done) {
                setTimeout(done, 1000);
            });
        });
    });
    

    如果您运行此程序,您将不会得到嵌套测试 bar 将被忽略,异步创建的测试 (delayed) 也将被忽略。

    Mocha 没有为这些类型的调用定义语义。当我在撰写本文时使用最新版本的 Mocha (2.3.3) 运行测试时,它只是忽略了它们。我记得早期版本的 Mocha 会识别测试,但会将它们附加到错误的 describe 块。

    【讨论】:

      【解决方案2】:

      我认为对动态测试的需求比较普遍(数据驱动测试?),动态it 和测试用例也很常见。

      我认为如果测试可以串行执行,那么管理测试用例的完成会更容易。这样您就不必担心管理嵌套的异步done。由于request 是异步的(我假设),您的测试用例仍将主要同时执行。

      describe('[test] enrichment', function () {
      
              var self = this;
      
      
              _.each(self.tests, function (json, cb) {
      
                  it('[test] ' + path.basename(json), function (done) {
      
                      var jsonDataForEnrichment = require(json);
                      jsonDataForEnrichment.customer.accountnum = "8497404620452729";
                      jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";
                      var options = {
                          url: self.serverURL + ':' + self.serverPort + '/event',
                          json: true,
                          body: jsonDataForEnrichment,
                          method: 'POST'
                      };
      
      
                     request(options,function (error, response, body) {
                          if (error) {
                              cb(error);
      
                          }
                          else{
                              assert.equal(response.statusCode, 201, "Error: Response Code");
                              cb(null);
                          }
      
                          done(); 
                      });
      
                  });
      
              }
          });
      

      【讨论】:

      • 使用 describe( ) 作为父代而不是 it ( ) 的一个问题是回调的上下文不同。嵌套 it( ) 更好,因为 'this' 指的是同一个东西..有点难以解释
      • 好吧,你是对的,嵌套 it() 会导致问题,而不是这样做
      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2015-04-04
      • 1970-01-01
      • 2019-07-26
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多