角度单位测试失败

时间:2018-06-08 19:48:46

标签: angular typescript angular5 karma-jasmine angular-unit-test

我正在尝试对函数editDependentInfo()进行单元测试,但是我收到了错误,因为在该函数中,正在调用另一个函数populateEditDepInfo()并且populateEditDepInfo具有this.dependentsInfo来自EditDependentInfo的属性和测试正在尝试访问但无法访问 请找到以下代码,让我知道该怎么做

let dependentsInfo: Dependents;
        class ProfileDependentsStub {
            dependentInfoChange: Subject<any[]> = new Subject<any[]>();
            dependentInfoDelete: Subject<any[]> = new Subject<any[]>();

            data = {
                body: [{states: [{name: "Alabama", code: "AL"}]}, {countries: [{name: "UNITED STATES", code: "USA"}]}, ['relationshipType'], {hideDBCardStatus:"no"}, {hsaenrollmentStatus: "Enrolled"},{beneficiaryType:["Contingent", "Primary"]},
                {relationshipType:["Adopted Child"]}, {status:["Active"]}, 
                {prefixlist: ["DEACON"]},{prefixlist: ["DEACON"]}, {addressDetails:[{addressLineOne: "6226 3RD DRIVE"}]}, {beneficiaryList:[{id:123, firstName:"David"}]},
                {dependentsList:[{id:1234, employeeId:909}]} ]}

            getChildObject = function (href) {
                return { href: '' };
            };

            getChildLinks = function () {
                return Observable.of(ResponseData);
            };

            getLabelList = function (label) {
                return label;
            };

            addChildObjects = function (links) {
                return links;
            };

            addLabelList = function (labels) {
                return labels;
            };

            getStateCountryList = function () {
                return Observable.of(this.data);
            };

            getRelationAndStatusList = function () {
                return Observable.of(this.data);
            };

            getPrefixSuffixList = function () {
                return Observable.of(this.data);
            };

            populateEditDepInfo = function(data){
                return data;
            }

            getDependentsInfoDetails = function () {
                this.data.body['dependentsList'] = new Array('John Smith');
                return Observable.of(this.data);
            };
        }

测试哪个失败

  it('should edit Dependent Info', () => {

        component.editDependentInfo(null,this.userDependentInfo)

        expect(component.isUpdateDependentInfo).toBeTruthy();
    });

我正在测试的组件

    export class {
       dependentsInfo: Dependents;
    editDependentInfo(event = null, userDependentInfo: Dependents) {
            this.dependentsInfo = <Dependents>{};
            this.clearSelectValues();
            this.isUpdateDependentInfo = true;
            this.dependentsInfo = userDependentInfo;
            this.populateEditDepInfo();
        }

        populateEditDepInfo(): void {
            this.saveDepInfo = false;
            if (this.dependentsInfo.id) {
                this.dependentId = this.dependentsInfo.id;
            }}
}

示例依赖类

class Dependents {
  name: string;
  id: string;
  dependentType: string;
  status: string;
  ineligibleReason: String;
  depndtDebitCardStatus: string;
  showCardStatusfInfo: boolean;
  addressLineOne: string;
  addressLineTwo: string;
  addressLineThree: string;
  city: string;}

我得到的错误

TypeError: Cannot read property 'id' of undefined
    at ProfileDependentsViewComponent.populateEditDepInfo (webpack:///./src/app/profile/_profile-dependents-view/profile-dependents-view.component.ts?:207:33)
    at ProfileDependentsViewComponent.editDependentInfo (webpack:///./src/app/profile/_profile-dependents-view/profile-dependents-view.component.ts?:202:14)
    at Object.eval (webpack:///./src/app/profile/_profile-dependents-view/profile-dependents-view.component.spec.ts?:135:19)

1 个答案:

答案 0 :(得分:1)

以下解决方案对我有用

it('should edit Dependent Info', () => {
let userDependentInfo= {  name: "john", ......}  as any

        component.editDependentInfo(null,this.userDependentInfo)

        expect(component.isUpdateDependentInfo).toBeTruthy();
    });