TypeError: Cannot read property 'length' of undefined AngularJS Karma
TypeError: Cannot read property 'length' of undefined AngularJS Karma
我有点卡在单元测试中,对于函数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)
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');
});
});
});
您有几个问题:
您的客户对象还没有地址:
customer = {
profile: {
firstName: 'John',
lastName: 'Smith',
phoneNumber: '55551234',
},
// no addresses!
};
所以当您调用 hasAddresses()
:
时,(customer.addresses).length
在第一个 expect
中失败
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 }];
我有点卡在单元测试中,对于函数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)
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');
});
});
});
您有几个问题:
您的客户对象还没有地址:
customer = {
profile: {
firstName: 'John',
lastName: 'Smith',
phoneNumber: '55551234',
},
// no addresses!
};
所以当您调用 hasAddresses()
:
(customer.addresses).length
在第一个 expect
中失败
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 }];