我有点卡在单元测试中,因为函数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');
});
});
});