When using the TestBed
, are you really unit testing a component or are you making integration tests?
Creating a fixture (TestBed.createComponent(AppComponent)
) and calling fixture.detectChanges()
automatically calls ngOnInit
. If you want to test another method, you are now testing multiple units.
This leads to another question: should you be testing units, or should you be testing user actions? For example, should you be testing the method setDimensions
or should you be testing that when the user clicks on a certain button, an element has the appropriate dimensions amongst other things.
I guess the first way of testing would be closer to the "unit test" way, but then you still have to deal with the lifecycle methods of the component being called. This makes me think there is no way to do unit tests of a component using TestBed
. Stubbing all lifecycle methods seems ridiculous.
Whichever way you decide to test, you should also test the DOM, should you not? Then you are not testing in isolation by including the DOM api.