2015-10-23 2 views
1

Как я могу создать рекурсивный тест chai из этого примера?Узел/Chai test loop recursive

Я хочу создать тест, который проходит через рекурсивный набор тестов, которые очень похожи. Представьте, что в викторине есть основные разделы и детские вопросы (которые могут быть n уровней глубокими), которые нуждаются в тестах. В настоящее время эти тесты просто вставляют данные. Разделы предназначены для группировки и других отчетов.

Ниже приведен краткий пример данных, а также текущий тест. Я подозреваю, что мне нужно использовать цикл обещаний для req, но я не уверен.

Вот несколько примеров кода с объектом викторины 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? 
      } 
     } 
     } 
    } 
    }); 
}); 

ответ

0

Для решения/хак Я создал счетчик, так как это тест и сделать обратный вызов делается после того, как цикл закончен.

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