TypeError: Cannot read property 'firstName' of undefined Unit Test AngularJS
TypeError: Cannot read property 'firstName' of undefined Unit Test AngularJS
我被困在用 Karma 做单元测试,我不知道如何进行单元测试,因为这是我第一次。我正在使用 AngularJS 并且单元测试是 Karma。
事情是这样的:我正在使用一项服务来获取客户的名字、姓氏和电话号码以显示在我的表格中,并且它可以正常工作,但是,当我尝试执行该单元时测试错误总是这样:
directionFormulation component should load customer profile FAILED
TypeError: Cannot read property 'firstName' of undefined
directionFormulation.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;
});
});
您正在将 order
注入控制器,因此您需要 "mock" 输出 order
进行单元测试:
describe('addressForm component', function () {
var component;
var scope;
var order;
beforeEach(function () {
bard.appModule('shopping.address');
bard.inject('$rootScope', '$componentController', '$q', 'resolveLocation', 'customer', 'event','order');
order = {
customer: {
firstName: 'Joe',
lastName: 'Smith',
phoneNumber: '416-555-1234'
}
};
scope = $rootScope.$new();
component = $componentController('addressForm', {
$scope: scope,
order: order
});
});
it('should be attached to the scope', function () {
expect(scope.addressForm).to.be.equal(component);
});
it('should load customer profile', function () {
component.$onInit();
component.loadCustomerProfile();
expect(component.input.firstName).to.be.equal(order.customer.firstName);
expect(component.input.lastName).to.be.equal(order.customer.lastName);
expect(component.input.phoneNumber).to.be.equal(order.customer.phoneNumber);
});
});
我想强调其他几个问题:
您的第一个测试断言 expect(scope.addressForm).to.be.equal(component);
不会通过。 AddressFormController
是你的controller的名字,一个controller就是一个组件上的属性。
我不确定 bard
在你的测试中指的是什么,也不确定 appModule
是否是你的 bard 实例上的 属性。这是我的组件测试设置示例:https://gist.github.com/mcranston18/0ded29eca9a53efeb945736b0a053061
我会推荐这个资源来学习更多关于测试组件控制器的知识:http://www.codelord.net/2017/01/09/unit-testing-angular-components-with-%24componentcontroller/
我被困在用 Karma 做单元测试,我不知道如何进行单元测试,因为这是我第一次。我正在使用 AngularJS 并且单元测试是 Karma。
事情是这样的:我正在使用一项服务来获取客户的名字、姓氏和电话号码以显示在我的表格中,并且它可以正常工作,但是,当我尝试执行该单元时测试错误总是这样:
directionFormulation component should load customer profile FAILED
TypeError: Cannot read property 'firstName' of undefined
directionFormulation.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;
});
});
您正在将 order
注入控制器,因此您需要 "mock" 输出 order
进行单元测试:
describe('addressForm component', function () {
var component;
var scope;
var order;
beforeEach(function () {
bard.appModule('shopping.address');
bard.inject('$rootScope', '$componentController', '$q', 'resolveLocation', 'customer', 'event','order');
order = {
customer: {
firstName: 'Joe',
lastName: 'Smith',
phoneNumber: '416-555-1234'
}
};
scope = $rootScope.$new();
component = $componentController('addressForm', {
$scope: scope,
order: order
});
});
it('should be attached to the scope', function () {
expect(scope.addressForm).to.be.equal(component);
});
it('should load customer profile', function () {
component.$onInit();
component.loadCustomerProfile();
expect(component.input.firstName).to.be.equal(order.customer.firstName);
expect(component.input.lastName).to.be.equal(order.customer.lastName);
expect(component.input.phoneNumber).to.be.equal(order.customer.phoneNumber);
});
});
我想强调其他几个问题:
您的第一个测试断言
expect(scope.addressForm).to.be.equal(component);
不会通过。AddressFormController
是你的controller的名字,一个controller就是一个组件上的属性。我不确定
bard
在你的测试中指的是什么,也不确定appModule
是否是你的 bard 实例上的 属性。这是我的组件测试设置示例:https://gist.github.com/mcranston18/0ded29eca9a53efeb945736b0a053061我会推荐这个资源来学习更多关于测试组件控制器的知识:http://www.codelord.net/2017/01/09/unit-testing-angular-components-with-%24componentcontroller/