gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'firstName' of undefined Unit Test AngularJS

转载 作者:行者123 更新时间:2023-12-03 03:23:12 25 4
gpt4 key购买 nike

我坚持用 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 注入(inject) Controller ,因此您需要为单元测试“模拟”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);
});
});

我想强调一些其他问题:

  1. 您的第一个断言 expect(scope.addressForm).to.be.equal(component); 的测试不会通过。 AddressFormController 是 Controller 的名称, Controller 是组件上的属性。

  2. 我不确定 bard 在您的测试中指代什么,也不确定 appModule 是否是您的 bard 实例上的属性。这是我的组件测试设置的示例:https://gist.github.com/mcranston18/0ded29eca9a53efeb945736b0a053061

  3. 我会推荐此资源来了解有关测试组件 Controller 的更多信息:http://www.codelord.net/2017/01/09/unit-testing-angular-components-with-%24componentcontroller/

关于javascript - 类型错误 : Cannot read property 'firstName' of undefined Unit Test AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46473285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com