node.js - Node/Chai 测试循环递归

标签 node.js automated-tests chai

如何从此示例创建递归 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? 
          }
        }
      }
    }
  });
});

最佳答案

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

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

关于node.js - Node/Chai 测试循环递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293836/

相关文章:

automated-tests - TestCafe - 如何运行多个装置(一个接一个,不并发)?

javascript - 如何期待与 Mocha 和 Chai 的 throw ?

javascript - 如何在调用时从 spy.on(object, 'funcName') 返回虚假数据?

node.js - 通过 API 网关安全路由的 ECS 容器未返回,但对容器的健康检查良好

node.js - 如何根据具有相对位置的共享库构建 nodejs C++ 插件

node.js - 访问 Azure 计费 API

node.js - 为什么 mongodb 对 mongo 的 localhost 实例的查询比对云实例的查询要快得多?

delphi - 在 TestComplete 脚本中使用 TTabSet

gradle - 如何使用命令行在Gradle中按类别运行测试?

node.js - 如何在循环中使用描述?