Node / Chai 测试循环递归

Node / Chai test loop recursive

我如何根据这个例子创建递归 chai 测试?

我想创建一个通过一组非常相似的递归测试运行的测试。想象一下,测验有需要测试的主要部分和子问题(可能有 n 级深)。目前这些测试只是插入数据。这些部分用于分组和其他报告。

下面是数据和当前测试的快速示例。我怀疑我需要为请求使用 promise 循环,但我不确定。

这是一些带有测验 json 对象的示例代码:

   describe('Create Responses for Quiz1 from student1', function() {
  var respondQuiz =  {
    "title": "School1 Quiz1 Title",
      "quizSections": [
      {
        "_id": "5629909fa7e71d9c1d9030ad",
        "title": "School1 Section1",
        "quizQuestions": [
          {
            "_id": "5629909fa7e71d9c1d9030b3",
            "title": "School1 Question2",
            "body": "<p>Do you have a Pet</p>",
            "quizQuestions": []
          },
          {
            "_id": "5629909fa7e71d9c1d9030c0",
            "title": "School1 Question2",
            "body": "<p>Do you have a Brother</p>",
            "quizQuestions": [
              {
                "_id": "5629909fa7e719c1d9030c1",
                "title": "School1 Question2",
                "body": "<p>Is he adopted?</p>",
                "quizQuestions": []
              }
            ]
          }
        ],
        "responseInstructions": "Give us your solution"
      },
      {
        "_id": "5629909fa7e71d9c1d9030ae",
        "title": "School1 Section2",
        "quizQuestions": [
          {
            "_id": "5629909fa7e71d9c1d9030bf",
            "title": "School1 Question3",
            "body": "<p>Do you have a Sister</p>",
            "quizQuestions": []
          }
        ]
      },
      {
        "_id": "5629909fa7e71d9c1d9030af",
        "title": "School1 Section3",
        "quizQuestions": [
          {
            "_id": "5629909fa7e71d9c1d9030c2",
            "title": "School1 Question4",
            "body": "<p>Do you have a Sister</p>",
            "quizQuestions": []
          }
        ]
      }
    ],
      "respondInstructions": "Here are your instructions",
      "_id": "5629909fa7e71d9c1d9030c8"
  };


  it('adds student1 Responses to school1quiz1 to database and responds with JSON:', function () {
    if (respondQuiz.quizSections && respondQuiz.quizSections.length > 0) {
      for (var i = 0; i < respondQuiz.quizSections.length; i++) {
        if (respondQuiz.quizSections[i].quizQuestions && respondQuiz.quizSections[i].quizQuestions.length > 0) {
          for (var x = 0; x < respondQuiz.quizSections[i].quizQuestions; x++) {

            //this is the test but I need to loop through the quizSection / quizQuestions.

            var response = {
              student: testData.students.student1.data._id,
              quiz: respondQuiz._id,
              quizSectionId: respondQuiz.quizSections[i]._id,
              quizQuestionId: respondQuiz.quizSections[i].quizQuestions[x]._id,
              description: 'This is student1 test response to ' + respondQuiz.quizSections[i].quizQuestions[x].title
            };

            var req = request(app)
              .post('/api/responses');
            req.set(jwtHelper.getAuthHeaders(testData.students.student1.token))
              .expect('Question-Type', /json/)
              .send({data: response})
              .expect(200)
              .end(function (err, res) {
                if (err) {
                  throw err;
                }
                assert.isObject(res.body.data);
                testData.response.student1School1Quiz1Response.data = res.body.data;
                assert.equal(testData.students.student1.data._id === testData.response.student1School1Quiz1Response.data.student, true);
                assert.equal(testData.quiz.school1Quiz.data._id === testData.response.student1School1Quiz1Response.data.quiz, true);
              });

            //done();  // This gets called to early because of the req is synchronous? 
          }
        }
      }
    }
  });
});

为了解决/破解,我创建了一个计数器,因为这是一个测试,并在循环结束后进行 done 回调。

          if (responseIndex === 12)  {
            done();
          }