Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Angular 2 Component with Constructor Parameter

Say I have an Angular 2 Component with two input parameters:

@Component{... (omitted for clarity)}
export class SomeComponent {

@Input() a: number
@Input() b: number

}

When I want to test this component I have something like:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        SomeComponent,
      ],
    })
    .compileComponents();
  }));

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

The createComponent call does not take any parameters or allow me to call the constructor. How can I instantiate/test the component for various number values?

like image 811
Boris van Katwijk Avatar asked Mar 07 '17 07:03

Boris van Katwijk


1 Answers

As pointed ou by JB Nizet, when a component has @input parameters, you need to init them in the beforeEach() : ```

beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;
    component.a = 1;
    component.b = 2;
    fixture.detectChanges();
});

```

like image 85
Deunz Avatar answered Nov 08 '22 18:11

Deunz