当前位置: 首页 > 知识库问答 >
问题:

为什么在Angular 10单元测试中FormControl没有被标记为dirty?

国跃
2023-03-14

以下测试在 Angular 9 中通过,但在 Angular 10 中失败。为什么?

upgrade-test.component.html:

<input [formControl]="formControl" />

升级-测试.组件. ts

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'app-upgrade-test',
  templateUrl: './upgrade-test.component.html'
})
export class UpgradeTestComponent {
  formControl: FormControl = new FormControl(0);
}

升级-测试.组件.规格:

import { TestBed } from '@angular/core/testing';
import { UpgradeTestComponent } from './upgrade-test.component';
import { ReactiveFormsModule } from '@angular/forms';

describe('Test', () => {
  TestBed.configureTestingModule({
    declarations: [UpgradeTestComponent],
    imports: [ReactiveFormsModule],
  });

  it('should pass', () => {
    const fixture = TestBed.createComponent(UpgradeTestComponent);
    const component = fixture.componentInstance;
    fixture.detectChanges();
    expect(component.formControl.dirty).toBeFalsy();

    component.formControl.patchValue(1);
    const inputElem: HTMLInputElement = fixture.nativeElement.querySelector('input');
    inputElem.dispatchEvent(new Event('change'));
    fixture.detectChanges();
    expect(component.formControl.dirty).toBeTruthy();
  });
});

测试结果:

  Test
    ✗ should pass
        Expected false to be truthy.
            at UserContext. (http://localhost:9876/_karma_webpack_/src/app/components/upgrade-test/upgrade-test.component.spec.ts:21:41)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:292:1)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:363:1)

共有1个答案

太叔京
2023-03-14

因为patchValue()setValue()方法不会更改控件或表单的状态。

如果用户更改了 UI 中的值,则控件是脏的。

https://angular.io/api/forms/AbstractControl#dirty

 类似资料:
  • 我目前正在与CDI Unit合作一个项目,我遇到了一个奇怪的问题。我试图在一个简单的项目中重现它: 我有一个使用CdiRunner运行的测试类(如下所述:http://jglue.org/cdi-unit-user-guide/我的测试类注入了被测试的单元:UUD。这个类扩展了一个超级类“ParentTestClass”,它目前是无用的。 测试课。爪哇: 正如我提到的,父类是空的。 ParentT

  • 以下是presenter中调用的方法: 下面是我的测试方法: 在Super.UpdateViewModel(vm)的处理程序中,我调用“SharedPreferenceHandler.MinStance.AccessToken!!)” 在我的测试方法中有可能得到这一点吗?

  • 问题内容: 在Java中,为什么以下代码行不起作用? 如果我将其更改为 起初,我以为您可能没有接口列表,但是我可以创建一个很好的接口。 有想法吗? 问题答案: 泛型类型比较古怪。 表示或任何子类型,但仅表示。如果您想要一个子类型,您需要 我怀疑你可以用 无法执行此操作的原因是,您可以使用对引用的引用,并且必须谨慎使用额外的间接级别。 使用泛型,您可以有两个间接级别,这会给您带来问题,因此它们更容易

  • 问题内容: 如何在JUnit 4中将测试标记为预期的失败? 在这种情况下,我想继续运行此测试,直到上游修补了某些内容。忽略测试有点太过分了,因为我可能会忘记它。我也许可以添加注释并捕获由引发的异常,但这似乎也与预期的行为有关。 这是我当前的测试结果: 该断言应该会成功,但是由于上游错误,它不会成功。然而,这个测试是正确的。它应该成功。实际上,我发现的所有替代方案都具有误导性。现在,我认为这是我最好

  • 从Spring 3.1开始,由于@Enable*注释,我们可以更容易地使用JavaConfig。 所以我做了一个WebConfig来设置WebMvc配置,并尝试对其进行测试。但是,如果我使用WebConfig扩展WebMVCConfigureAdapter或WebMvcConfigurationSupport,单元测试将失败,因为缺少ServletContext。代码和消息如下所示。 网络配置。J

  • 我还可以用 我得到一条消息,说测试通过了,但从未显示在屏幕上。为什么不呢?