jeudi 19 mai 2016

jasmine undefined not an object, can't find key

If I make the respond object from the httpbackend a single object instead of an array for 'data' it works fine. But once I make it an array (even single element array) it doesn't want to work and gives me an error saying undefined is not an object (evaluating 'booking.Student.studentName'). Below is the code. Any ideas? Is it because lodash isn't doing its thing?

I have the following function in my controller

$scope.initBookings = function() {
  return $http.get('/api/bookings').then(function(response) {
    return $scope.bookings = _.map(response.data, function(booking) {
      booking.studentName = booking.Student.studentName;
      booking.slotDay = booking.Slot.day;
      booking.slotTime = booking.Slot.time;
      booking.subjectName = booking.Subject.name;
      return booking;
    });
  });
};

And my test is the following

it('should store bookings into $scope.bookings and create a $scope.bookingsTable', function() {
  expect(this.scope.bookings).toBeUndefined();
  this.scope.initBookings();
  this.httpBackend.expect('GET', '/api/bookings');
  this.httpBackend.flush();
  expect(this.scope.bookings).toBeDefined();
});

this.httpBackend.whenGET('/api/bookings').respond(function() {
  return [
    200, {
      data: [
        {
          Student: {
            studentName: 'John Die'
          },
          Slot: {
            day: 'today',
            time: 'now'
          },
          Subject: {
            name: 'maths'
          }
        }, {
          Student: {
            studentName: 'Jane Die'
          },
          Slot: {
            day: 'tomorrow',
            time: 'before'
          },
          Subject: {
            name: 'mathamatix'
          }
        }
      ]
    }
  ];
});

Aucun commentaire:

Enregistrer un commentaire