首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对ngx进行单元测试?

如何对ngx进行单元测试?
EN

Stack Overflow用户
提问于 2021-07-28 10:12:15
回答 1查看 830关注 0票数 1

我正在尝试正式地对ngx进行单元测试,但我得到了以下错误:

错误验证器"emailValidator“无法找到。请确保这是通过FormlyModule声明注册的。

我还检查了棱角格式的文档,他们以同样的方式编写了代码。

文档:

代码语言:javascript
复制
FIELD WITH CUSTOM VALIDATION
You just need to include the name of the validate function, declared in FormlyModule, within the property validators.validation.

{
  key: 'ip',
  type: 'input',
  templateOptions: {
    label: 'IP Address (using custom validation declared in ngModule)',
    required: true,
  },
  validators: {
    validation: ['ip'],
  },
},

文档链接:https://formly.dev/guide/validation

**我的代码**

form.component.ts:

代码语言:javascript
复制
export class FormComponent {
  fields: FormlyFieldConfig[] = [
    {
      key: 'email',
      type: 'input',
      templateOptions: {
        label: 'Email address',
        placeholder: 'Enter email',
        type: 'email',
        required: true,
        attributes: {
          autocapitalize: 'off',
          autocorrect: 'off',
          autocomplete: 'off',
        },
      },
      validators: {
        validation: ['emailValidator'],
      },
    },
  ];

form.component.spec.ts:

代码语言:javascript
复制
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { FormlyModule } from '@ngx-formly/core';
import { FormlyMaterialModule } from '@ngx-formly/material';

import { FormComponent } from './form.component';

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

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ FormComponent],
      imports: [
        FormlyModule.forRoot(),
        FormlyMaterialModule,
        ReactiveFormsModule,
      ]
    })
    .compileComponents();
  });

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });

});

有人能指点我吗?任何线索都会有帮助的!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 12:26:03

请您共享您的App.module文件内容好吗?您的"emailValidator“必须以下列方式在app.module文件的导入部分注册:

代码语言:javascript
复制
imports: [
FormlyModule.forRoot({
   validators: [
                { name: 'emailvalidator', validation: validator_function_name },
               
              ]
           )}

请查一下这里,https://formly.dev/examples/validation/custom-validation

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68558444

复制
相关文章

相似问题

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