title: TypeError: Cannot read property 'firstName' of undefined Unit Test AngularJS
body: <p>I am stuck to do the Unit Test with Karma, I don't have idea how to the unit test because it's my first time. I am using AngularJS and the unit test is Karma.</p>
  
  <p>The thing is this: I'm using a service to obtain the firstName, lastName and PhoneNumber of the customer to show in my form, and it works without any problem BUT, when I'm trying to do the unit test the error is always this: </p>
  
  <pre><code>directionFormulation component should load customer profile FAILED
          TypeError: Cannot read property 'firstName' of undefined
  </code></pre>
  
  <p>directionFormulation.js</p>
  
  <pre><code>  function directionFormulationController(event, customer, resolveLocation, order) {
      this.$onInit = onInit;
      this.input = this.input || {};
  
      function onInit() {
        loadCustomerData();
      }
  
      function loadCustomerData() {
        this.input.firstName = order.customer.firstName;
        this.input.lastName = order.customer.lastName;
        this.input.phoneNumber = order.customer.phoneNumber;
  
      }
    }
  })();
  </code></pre>
  
  <p>Unit test: directionFormulation.spec.js:</p>
  
  <pre><code>  it('should load customer data', function () {
      var emptyFirstName = { firstName: 'something'};
  
      component.$onInit();
      order.customer.firstName = { firstName: 'Something'};
      order.customer.lastName = { lastName: 'Something' };
      order.customer.phoneNumber = { phoneNumber: 55555555};
      // component.input = {
      //   firstName: 'something',
      //   lastName: 'something',
      //   phoneNumber: 55555555
      // };
  
      component.loadCustomerData();
      $rootScope.$apply();
      component.input.firstName = newFirstName;
  
      expect(component.input.firstName).to.be.equal({firstName: 'something'});
      expect(component.input.lastName).to.be.not.empty;
      expect(component.input.phoneNumber).to.be.null;
  
    });
  });
  </code></pre>