dimanche 22 novembre 2015

Angular/Karma: Mock Data Undefined

I'm trying to create a unit test that checks if a GET request returns the correct amount of items, but I'm using mock data to do this.

My test looks like this:

test.js

describe('Customer Controller', function () {
    var controller;
    var customers = mockData.getMockCustomers(); // fake customers (5 customers)

    beforeEach(function() {
      bard.appModule('app');
      bard.inject('$controller', '$q', '$rootScope');

      var cs = {
        getCustomers: function() {
          return $q.when(customers);
        }
      };

      controller = $controller('scoreListCtrl', {
        CustomerService: cs
      });
    });

    it('should return 5 customers', function() {
      $rootScope.$apply();
      expect(controller.customers).to.have.length(5);
    });
});

I keep getting this error when I run the test:

TypeError: Cannot read property 'length' of undefined

It looks like controller.customers is coming back as undefined for some reason. Am I mocking the data correctly?

I'm very new to this and can't work out what I'm doing wrong. I don't even know how to debug such an issue.

Any help is appreciated. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire