使用Jest进行Angular 9测试-为什么该mat-checkbox测试失败?

时间:2020-05-12 10:15:27

标签: angular testing jestjs

我的组件中有一个重置功能,可以重置所有子复选框:

  it('should uncheck all checkboxes when reset is called', fakeAsync(() => {
    const checkboxInput = fixture.debugElement.query(
      By.css('.mat-checkbox-input')
    );
    const el = checkboxInput.nativeElement;
    el.click();
    component.reset();
    fixture.detectChanges();
    expect(el.checked).toBe(false);
  }));

这是测试:

console.log

我从tick()看到重置函数被调用并且复选框为true。但是到了期望值运行的时候,当它应该为假时仍然是真的。

我尝试在reset()调用之后添加一个 it('should uncheck all checkboxes when reset is called', async(() => { const checkboxInput = fixture.debugElement.query( By.css('.mat-checkbox-input') ); const el = checkboxInput.nativeElement; el.click(); component.reset(); fixture.detectChanges(); fixture.whenStable().then(() => { expect(el.checked).toBe(false); }); })); ,但这没什么区别。

更新

我发现了this问题,除了他们直接更改测试内部组件的复选框绑定属性外,该问题几乎相同。但是无论如何我都尝试过:

---
- name: Install business service
  hosts: business
  vars:
    app_name: "e-service"
    app: "{{ app_name }}-{{ tag }}.war"
    app_service: "{{ app_name }}.service"
    app_bootstrap: "{{ app_name }}_bootstrap.yml"
    app_folder: "{{ eps_client_dir }}/{{ app_name }}"
    archive_folder: "{{ app_folder }}/archives/arch_{{ansible_date_time.date}}_{{ansible_date_time.hour}}_{{ansible_date_time.minute}}"
    app_distrib_dir: "{{ eps_distrib_dir }}/{{ app_name }}"
    app_dependencies: "{{ app_distrib_dir }}/dependencies.tgz"

  tasks:

   - name: Copy app {{ app }}  to {{ app_folder }}
    copy:
     src: "{{ app_distrib_dir }}/{{ app }}"
     dest: "{{ app_folder }}/{{ app }}"
     group: ps_group
     owner: ps
     mode: 0644
    notify:
       - restart app

  - name: Copy service setting to /etc/systemd/system/{{app_service}}
    template:
     src: "{{ app_distrib_dir }}/{{ app_service }}"
     dest: /etc/systemd/system/{{ app_service }}
     mode: 0644
    notify:
       - restart app


  - name: Start service {{ app }}
    systemd:
     daemon-reload: yes
     name: "{{ app_service }}"
     state: started
     enabled: true

  handlers:
    - name: restart app
      systemd:
        daemon-reload: yes
        name: "{{ app_service }}"
        state: restarted
        enabled: true

但这也失败了。

1 个答案:

答案 0 :(得分:0)

问题是时机之一,因此测试需要detectChanges进行两次。

这可行:

  it('should uncheck all checkboxes when reset is called', () => {
    const checkboxInput = fixture.debugElement.query(
      By.css('.mat-checkbox-input')
    );
    const el = checkboxInput.nativeElement;
    el.click();
    fixture.detectChanges();
    component.reset();
    fixture.detectChanges();
    expect(el.checked).toBe(false);
  });

这样做:

  it('should uncheck all checkboxes when reset is called', async(() => {
    const checkboxInput = fixture.debugElement.query(
      By.css('.mat-checkbox-input')
    );
    const el = checkboxInput.nativeElement;
    el.click();
    fixture.detectChanges();
    component.reset();
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      expect(el.checked).toBe(false);
    });
  }));