gpt4 book ai didi

angular - 如何在 Angular 单元测试中修复 'Error: No value accessor for form control with name'?

转载 作者:行者123 更新时间:2023-12-03 22:48:23 26 4
gpt4 key购买 nike

我无法测试使用带有 ngModel 的自定义组件的组件
HTML 代码如下所示(在下面的 repro 中查看更多信息)

<custom-control name="formCode" [(ngModel)]="testValue"></custom-control>
该代码在我的应用程序中运行,但在我的测试中失败并出现以下错误

Uncaught Error: Uncaught (in promise): Error: No value accessor for form control with name: 'formCode'

Error: No value accessor for form control with name: 'formCode'


测试是用 Jasmine 运行的
我尝试了不同的导入,但似乎没有一个能解决我的问题
复制代码在这里: https://stackblitz.com/edit/angular-test-ng-model

最佳答案

就我而言,我使用的是未在单元测试文件中导入的自定义 Angular 组件 (SingleSelectModule)。我希望这可以帮助遇到与我相同问题的其他人:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule } from '@angular/forms';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { CaselistComponent } from './caselist.component';
import { SingleSelectModule } from '@uimf/uitk/components/single-select';

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

beforeEach(async(() => {


TestBed.configureTestingModule({
declarations: [
...
],
imports: [
FormsModule,
SingleSelectModule,
RouterTestingModule,
HttpClientTestingModule

],
schemas: [CUSTOM_ELEMENTS_SCHEMA],

}).compileComponents();

});

});

关于angular - 如何在 Angular 单元测试中修复 'Error: No value accessor for form control with name'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57168077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com