【问题标题】:Setting initial values to Angular form group for testing将初始值设置为 Angular 表单组以进行测试
【发布时间】:2019-05-26 08:16:22
【问题描述】:

我正在为 Angular 7 响应式表单编写单元测试,该表单的输入值预先填充了 ngInit 上的服务器数据。如何设置此表单的值,使其在测试期间不会出现“值未定义”错误?

这是我在 ngOnInit 上预先填写的表单:

 ngOnInit() {
  this.userForm = this.formBuilder.group({
  id: [ this.user.id ],
  first_name: [this.user.first_name, Validators.required],
  last_name: [this.user.last_name, Validators.required],
});

这是表单的当前(准系统)测试代码:

//import modules

describe('UserListItemComponent', () => {
  let component: UserListItemComponent;
  let fixture: ComponentFixture<UserListItemComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
    imports: [
      MaterialModule,
      ReactiveFormsModule,
      FormsModule,
    ],
    declarations: [
      UserListItemComponent,
    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
  })
  .compileComponents();
  }));

beforeEach(() => {
  fixture = TestBed.createComponent(UserListItemComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

it('should create', () => {
  component.userForm.setValue({
    id: 123,
    first_name: "john",
    last_name: "smith",
  });
  expect(component).toBeTruthy();
  });
});

但是,Karma 测试运行器仍然说它对于 id 是未定义的。是不是因为没有用setValue预填充数据?

【问题讨论】:

    标签: javascript angular unit-testing


    【解决方案1】:

    调用fixture.detectChanges(); 将调用ngOnInit(),但user 属性未定义。您可以像这样更改代码:

    fixture = TestBed.createComponent(UserListItemComponent);
    component = fixture.componentInstance;
    const user = {
    //your properties here
    };
    component.user = user;
    fixture.detectChanges();
    

    【讨论】:

      【解决方案2】:

      您可以在测试时使用更新表单值的功能:

      function updateForm(id: string, first_name: string, last_name: string) {
              component.userForm.controls['id'].setValue(id);
              component.userForm.controls['first_name'].setValue(first_name);
              component.userForm.controls['last_name'].setValue(last_name);
          }
      

      在测试用例中:

       it('should create form', () => {
        updateForm('1','john','smith');
        expect(component).toBeTruthy();
        expect(component.userForm.value).toBeTruthy();
      });
      

      或者你可以将user 对象传递给组件使用:

      `component.user = mockUser;`
      

      并且表单值将在初始化时设置。

      很可能是由于组件上未定义的user 属性而导致的错误。

      【讨论】:

      • 感谢您的回答,不幸的是它不起作用。我收到“无法读取未定义的属性‘控件’”错误。当我登录 fixture.component 时,它会返回:{"formBuilder":{},"isEditing":false,"select":{"_isScalar":false,"observers":[],"closed":false,"isStopped":false,"hasError":false,"thrownError":null,"__isAsync":false},"delete":{"_isScalar":false,"observers":[],"closed":false,"isStopped":false,"hasError":false,"thrownError":null,"__isAsync":false},"edit":{"_isScalar":false,"observers":[]}
      • id: [ this.user.id ] 中,使用@input 注入用户。这与我的错误有关吗?
      • 如果是这种情况,您可以模拟必要的数据来创建您的form。确保你的组件收到它需要的东西onInit
      猜你喜欢
      • 2020-08-03
      • 2019-12-28
      • 2018-08-07
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2018-05-22
      相关资源
      最近更新 更多