Node / Chai测试循环递归

时间:2015-10-23 02:18:54

标签: node.js automated-tests chai

如何从此示例创建递归chai测试?

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

以下是数据和当前测试的快速示例。我怀疑我需要为req使用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? 
          }
        }
      }
    }
  });
});

1 个答案:

答案 0 :(得分:0)

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

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