How can I mock a ControlContainer
instance so that I can test my component?
I have a child component that injects a ControlContainer
into the constructor, so its usage is
<acr-score-card formGroupName="score"></acr-score-card>
and the component itself is
@Component({
selector: 'acr-score-card',
templateUrl: './score-card.component.html',
styleUrls: ['./score-card.component.scss']
})
export class ScoreCardComponent implements OnInit {
...
form: FormGroup;
constructor(private ngControl: ControlContainer) { }
ngOnInit() {
this.form = <FormGroup>this.ngControl.control;
}
...
}
Everything works fine when I run in the browser but I cannot get the unit tests to work as I am not sure how to mock the ControlContainer
instance in order to setup the provider, this is the contents of my spec file:
describe('ScoreCardComponent', () => {
let component: ScoreCardComponent;
let fixture: ComponentFixture<ScoreCardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TestingModule],
declarations: [ScoreCardComponent],
providers: [/** what goes here to mock the ControlContainer */]
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ScoreCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
So, to repeat the question, how can I mock a ControlContainer
instance so that I can test my component?
ControlContainer
is an interface, you need create instance with the classes that implement the interface likeFormGroupDirective
etc. – Entire