'Test that we correctly capture exception messages that bubble up from
grading. Note that we only see errors at this level if the grading
process for this student fails entirely due to an unexpected event --
having errors in the problem sets will not trigger this.
We patch the grade() method with our own, which will generate the errors
for student3 and student4.'

@patch('lms.djangoapps.grades.new.course_grade_factory.CourseGradeFactory.create')
def test_grading_exception(self, mock_course_grade):

    (student1, student2, student3, student4, student5) = self.students
    mock_course_grade.side_effect = [(Exception('Error    for    {}.'.format(student.username)) if (student.username in ['student3', 'student4']) else mock_course_grade.return_value) for student in self.students]
    with self.assertNumQueries(4):
        (all_course_grades, all_errors) = self._course_grades_and_errors_for(self.course, self.students)
    self.assertEqual({student: all_errors[student].message for student in all_errors}, {student3: 'Error    for    student3.', student4: 'Error    for    student4.'})
    self.assertEqual(len(all_course_grades), 5)
    self.assertIsNone(all_course_grades[student3])
    self.assertIsNone(all_course_grades[student4])
    self.assertIsNotNone(all_course_grades[student1])
    self.assertIsNotNone(all_course_grades[student2])
    self.assertIsNotNone(all_course_grades[student5])
