角单元测试:CUSTOM_ELEMENTS_SCHEMA错误

时间:2018-11-01 14:26:51

标签: angular unit-testing jasmine karma-jasmine

在单元测试中遇到麻烦。我有一个新的项目,要求帮助修复测试,我添加了“ CUSTOM_ELEMENTS_SCHEMA”,以便在子组件中角度不会更深,并且出现此错误,原因是我不清楚为什么。

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();
  });
});

2 个答案:

答案 0 :(得分:0)

简单易用

不是导入而是“方案”

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

答案 1 :(得分:0)

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

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

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

然后在CheckboxComponent中输入“方案:[CUSTOM_ELEMENTS_SCHEMA]”。