0

我有点卡在单元测试中,因为函数hasAddress()但它似乎不起作用,我想我不知道如何用 Karma 进行 angularjs 单元测试,但我不知道了解如何解决这部分,我不确定我错了什么,有人可以帮助我吗?

错误是:

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)

方向公式.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;
      });
    }

单元测试方向Formulation.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');
    });
  });
});
4

1 回答 1

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 }];
于 2017-10-04T17:49:18.677 回答