TypeError:无法读取未定义的AngularJS Karma的属性“length”

时间:2017-10-04 17:32:46

标签: angularjs unit-testing karma-mocha gulp-karma

我在单元测试中有点困难,对于函数 hasAddress()但似乎它不起作用,我想我不知道怎么做angularjs单元测试与Karma,但我不明白如何解决这个部分,我不知道我错了什么,谁能帮助我?

错误是:

HeadlessChrome 0.0.0 (Mac OS X 10.12.6) directionFormulation component should load customer customerData FAILED
        TypeError: Cannot read property 'lexngth' of undefined
            at hasAddresses (app/Resources/assets/workplace/js/directionFormulation.js:9:4128)
            at directionFormulationController.onInit [as $onInit] (app/Resources/assets/workplace/js/directionFormulation.js:9:2344)
            at Context.<anonymous> (app/Resources/assets/workplace/js/test/specs/directionFormulation.spec.js:241:15)
HeadlessChrome 0.0.0 (Mac OS X 10.12.6) directionFormulation component should be checking if has addresses FAILED
        TypeError: Cannot read property 'length' of undefined
            at directionFormulationController.hasAddresses (app/Resources/assets/workplace/js/directionFormulation.js:9:4128)
            at Context.<anonymous> (app/Resources/assets/workplace/js/test/specs/directionFormulation.spec.js:261:22)

directionFormulation.js

    function customerData() {
      return customer.loadCustomerData().then(function (customerData) {
        this.customerDataCustomer = customerData;

        this.input.firstName = this.customerDataCustomer.firstName;
        this.input.lastName = this.customerDataCustomer.lastName;
        this.input.phoneNumber = this.customerDataCustomer.phoneNumber;
      });
    }

单元测试directionFormulation.spec.js

 it('should load customer customerData', function () {
    component.$onInit();
    component.customerData();
    // component.loadCustomerData().then(function () {
    //   expect(component.input.firstName).to.be.equal(customer.customerData.firstName);
    //   expect(component.input.lastName).to.be.equal(customer.customerData.lastName);
    //   expect(component.input.phoneNumber).to.be.equal(customer.customerData.phoneNumber);
    // });
    sinon.stub(customer, 'loadCustomerData').returns($q.when({firstName: 'John', lastName: 'Smith', phoneNumber:'55551234'}));
    component.loadRegions().then(function () {
      expect(customer.customerData.firstName).to.exist;
      expect(customer.customerData.lastName).to.exist;
      expect(customer.customerData.phoneNumber).to.exist;
      expect(customer.customerData.firstName).to.be.equals('John');
      expect(customer.customerData.lastName).to.be.equals('Smith');
      expect(customer.customerData.phoneNumber).to.be.equals('55551234');
    });
  });
});

1 个答案:

答案 0 :(得分:1)

您有几个问题:

您的客户对象尚未包含地址:

customer = {
  profile: {
    firstName: 'John',
    lastName: 'Smith',
    phoneNumber: '55551234',
  },
  // no addresses!
};

所以当您致电(customer.addresses).length时,expecthasAddresses()中失败:

expect(component.hasAddresses()).to.be.false;

也许您的hasAddresses函数应该是这样的:

function hasAddresses() {
  return customer.addresses && customer.addresses.length;
}

在以下expect之后,您仍然会遇到问题,因为您要将地址创建为对象:

customer.addresses = { 1: { id: 1 } };

它应该是一个数组:

customer.addresses = [{ id: 1 }];