首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >执行Angular Unit测试时出错

执行Angular Unit测试时出错
EN

Stack Overflow用户
提问于 2018-06-07 03:39:42
回答 1查看 908关注 0票数 2

我在做Angular单元测试时遇到了一个错误。我正在尝试测试创建组件请参阅下面的错误文件,让我知道为什么我面对它,我还导入了与提供程序相关的服务和模块的导入。

下面的错误是Karma在单元测试时显示的。我想我漏掉了什么

TypeError: Cannot read property 'userName' of undefined
at Object.eval [as updateRenderer] (ng:///DynamicTestModule/ProfilePersonalInformationComponent.ngfactory.js:72:38)
at Object.debugUpdateRenderer [as updateRenderer] (webpack:///./node_modules/@angular/core/esm5/core.js?:14909:21)
at checkAndUpdateView (webpack:///./node_modules/@angular/core/esm5/core.js?:14023:14)
at callViewAction (webpack:///./node_modules/@angular/core/esm5/core.js?:14369:21)
at execEmbeddedViewsAction (webpack:///./node_modules/@angular/core/esm5/core.js?:14327:17)
at checkAndUpdateView (webpack:///./node_modules/@angular/core/esm5/core.js?:14019:5)
at callViewAction (webpack:///./node_modules/@angular/core/esm5/core.js?:14369:21)
at execEmbeddedViewsAction (webpack:///./node_modules/@angular/core/esm5/core.js?:14327:17)
at checkAndUpdateView (webpack:///./node_modules/@angular/core/esm5/core.js?:14019:5)
at callViewAction (webpack:///./node_modules/@angular/core/esm5/core.js?:14369:21)

为了解决上述错误,我创建了PerosnalInfoStub类,但它不起作用。

component.spec.ts文件

describe('ProfilePersonalInformationComponent', () => {
  let component: ProfilePersonalInformationComponent;
  let fixture: ComponentFixture<ProfilePersonalInformationComponent>;
  let personalInfo : PersonalInfo;

  class PersonalInfoStub{
      personalInfo: Subject<any[]> = new Subject<any[]>();



  }

  beforeEach(async(() => {
    TestBed.configureTestingModule({

        imports: [FormsModule, SharedModule, HttpModule, BrowserModule],
      declarations: [ ProfilePersonalInformationComponent ],
      providers: [
        {
           provide: PersonalInfo, useClass: PersonalInfo
        },

        {
            provide: NotificationService, useClass: NotificationService
        },

        {
            provide: LoginService, useClass: LoginService
        },
        {
            provide: ConfigService, useClass: ConfigService
        }

    ],


    })
    .compileComponents();
  }));

  beforeEach(() => {

    fixture = TestBed.createComponent(ProfilePersonalInformationComponent);
    component = fixture.componentInstance;

    fixture.detectChanges();
  });

  it('should create', () => {

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

personal-info.ts

export class PersonalInfo {
  userName: string;
}

组件的类文件

ProfilePersonalInformationComponent implements OnInit {




  @Input() personalInfo: PersonalInfo;
  @Input() loadingData;
  savingData: boolean;

  passwordHelpTextArray: string[];
  passwordHelpText: string;
  formErrors: any = {
    username: {
      error: '',
      errorMessage: ''
    },
    currentPassword: {
      error: '',
      errorMessage: ''
    },
    newPassword: {
      error: '',
      errorMessage: ''
    },
    verifyNewPassword: {
      error: '',
      errorMessage: ''
    }
  };

  updatedUsername: string = '';
  existingPassword: string = '';
  newPassword: string = '';
  reEnterNewPassword: string = '';

  constructor(private personalInfoService: PersonalInformationService,
    private notificationService: NotificationService) { }

  ngOnInit(): void {
    this.populateInfo();
  }

  populateInfo() {
    setTimeout(() => {
      if (this.loadingData === false) {
        this.updatedUsername = this.personalInfo.userName;
      } else {
        this.populateInfo();
      }

    }, 500);
  }

用户名的Html代码

 <div class="col-sm-2">
        <h3>Username</h3>
        <p>{{personalInfo.userName}}</p>
      </div>
      <div class="col-sm-2">
        <h3>Password</h3>
        <p>********</p>
      </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 03:51:19

您应该在测试时模拟组件数据,特别是@Input属性,它通常会从父组件获取值。但在单元测试中,您必须自己模拟它,因为不涉及父组件。

beforeEach(() => {
    fixture = TestBed.createComponent(ProfilePersonalInformationComponent);
    component = fixture.componentInstance;
    // mock input data
    component.personalInfo = {"userName" : "XYZ"};
    fixture.detectChanges();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50728302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档