我坚持使用 Karma 进行单元测试,我不知道如何进行单元测试,因为这是我的第一次。我正在使用 AngularJS,单元测试是 Karma。
事情是这样的:我正在使用一项服务来获取要在我的表单中显示的客户的名字、姓氏和电话号码,并且它可以正常工作但是,当我尝试进行单元测试时,错误总是这:
directionFormulation component should load customer profile FAILED
TypeError: Cannot read property 'firstName' of undefined
方向公式.js
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;
}
}
})();
单元测试:directionFormulation.spec.js:
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;
});
});