【问题标题】:Angular Unit test: CUSTOM_ELEMENTS_SCHEMA Error角度单元测试:CUSTOM_ELEMENTS_SCHEMA 错误
【发布时间】:2018-11-01 14:26:51
【问题描述】:

单元测试有问题。我有一个新项目要求帮助修复测试,我正在添加“CUSTOM_ELEMENTS_SCHEMA”,这样 Angular 就不会在子组件中更深入,并且我在下面收到此错误,我不清楚原因。

Unexpected value 'custom-elements' imported by the module 'DynamicTestModule'

此处的组件规格

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

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

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

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

【问题讨论】:

    标签: angular unit-testing jasmine karma-jasmine


    【解决方案1】:

    简单易行

    不是导入而是“模式”

     TestBed.configureTestingModule({
            schemas: [NO_ERRORS_SCHEMA],
            declarations: [ Component ]
        })
        .compileComponents();
    

    【讨论】:

      【解决方案2】:

      您从 TestBed.configureTestingModule 的导入中删除了 CUSTOM_ELEMENTS_SCHEMA。并添加 TestBed.configureTestingModule 的 schemas

      TestBed.configureTestingModule(
              imports: [FormsModule, ReactiveFormsModule],
              declarations: [ CheckboxComponent ],
              schemas: [CUSTOM_ELEMENTS_SCHEMA]
          })
          .compileComponents();
        }));
      

      如果您在 CheckboxComponent 类中导入,请在 TestBed.configureTestingModule 中导入相同的模块。

      并在 CheckboxComponent 中写入“架构:[CUSTOM_ELEMENTS_SCHEMA]”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-22
        • 2018-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-05
        • 1970-01-01
        相关资源
        最近更新 更多