DEV Community

Discussion on: Unit Testing in Angular - To TestBed or NOT to TestBed

Collapse
 
martinspire profile image
Martin Spierings

I recently started in a team that is all-in on blackbox testing. So there's no component logic in my tests, just the DOM and everything you do is interacting with the DOM. This means that you need testbed, but that also means that for testing, you don't really care what happens in the controller because ultimately it doesn't matter. It does make it a bit more difficult to simply test certain functions but if it never modifies the DOM do you really need to have it in your component?

We also use NG-Spectator and NG-Mocks to do stubbing of children, services and what have ya which makes it a lot easier to write tests. I really hope the Angular team considers using these modules (or their logic) to upgrade Testbed at some point in the future because its mighty handy. I also prefer it over the use of TypeMoq (as its much easier to read imo).

Without testbed, as you demonstrated, your DOM could be anything and you aren't really testing components. Its great for testing services, but terrible to check whether what the user sees is what you developed. The HTML is still part of what makes angular tick and if you skip that part your tests aren't going to be reliable