简体   繁体   English

使用primeng进行单元测试

[英]Unit test with primeng

I try to write my first unit test on a component in which I used some primeng components. 我尝试在我使用了一些primeng组件的组件上编写我的第一个单元测试。

When I run "ng test" I got this error : 当我运行“ng test”时,我收到了这个错误:

Chrome 63.0.3239 (Linux 0.0.0) HeaderComponent should create FAILED
    1. If 'p-dropdown' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.
    2. If 'p-dropdown' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("header-text">
            <p-dropdown [options]="languages" (onChange)="onDropdownChange($event.value)" [ERROR ->][(ngModel)]="selectedLanguage"></p-dropdown>
        </div>

Not sure about what I need to do? 不确定我需要做什么? Do I need to inject or mock anything? 我需要注射或模拟任何东西吗?

Here my unit test (basically the generated one) : 这是我的单元测试(基本上是生成的):

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [],
      declarations: [HeaderComponent]

    })
      .compileComponents();
  }));

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

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

Thx 谢谢

Add NO_ERRORS_SCHEMA in schemas, This is a way to ignore child components while unit testing. 在模式中添加NO_ERRORS_SCHEMA,这是一种在单元测试时忽略子组件的方法。

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [DropdownModule],
    declarations: [ HeaderComponent ],
    schemas: [NO_ERRORS_SCHEMA]   // <-- Add This Line. (make sure to import NO_ERRORS_SCHEMA)
  })
  .compileComponents();
}));

Sure, you add the NO_ERROR_SCHEMA to ignore child components. 当然,您添加NO_ERROR_SCHEMA以忽略子组件。 In case you want to use the slider in a test it is better to mock it. 如果你想在测试中使用滑块,最好是模拟它。 Lib called ngMocks is the most common way to mock it out and it has the ability to assert on it's inputs or emit on an output to assert on a side effect. 称为ngMocks的 lib是最常见的模拟方法,它能够在输入上断言或在输出上发出以断言副作用。

ngMocks can be adde via npm: npm i ng-mocks ngMocks可以通过npm: npm i ng-mocks

For example mocking out p-slider component would look like this: 例如, p-slider组件将如下所示:

 import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {MockComponent} from 'ng-mocks'; //step 1: add mock util function import {Slider, SliderModule} from 'primeng/slider'; // setp 2: Mocked component and it's module import {DateRangePickerComponent} from './date-range-picker.component'; describe('DateRangePickerComponent', () => { let component: DateRangePickerComponent; let fixture: ComponentFixture<DateRangePickerComponent>; beforeEach( async(() => { TestBed.configureTestingModule({ imports: [SliderModule], // add mocked comp's module declarations: [ DateRangePickerComponent, MockComponent(Slider) //actual mocking ] }).compileComponents(); }) ); beforeEach(() => { fixture = TestBed.createComponent(DateRangePickerComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); }); 

Try importing the module in your testbed: 尝试在测试平台中导入模块:

import { DropdownModule } from 'primeng/primeng';
describe('HeaderComponent', () => {
  let component: HeaderComponent;
  let fixture: ComponentFixture<HeaderComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [DropdownModule],
      declarations: [HeaderComponent]

    })
      .compileComponents();
  }));

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM