DEV Community

Discussion on: How to write unit tests

Collapse
 
jeremyf profile image
Jeremy Friesen

How difficult do you find learning how to test?

When I first saw unit tests (Rails 1 or so) I chose to not learn both Rails and how to write tests at the same time. I chose to write Rails. That was a mistake. Had I focused on writing tests, I would've learned Rails along the way and had a better understanding of the implications.

These days, I often dive into unit tests to help me think through the problem. This involves mocks, stubs, and dependency injection (my preference).

So, it's a bit disheartening to hear that there aren't good resources for learning tests.

Collapse
 
marcinwosinek profile image
Marcin Wosinek

For learning unit tests I would try doing radical TDD for few weeks. Radical, as in not writing a single line of code, unless you have a test that fails when the line is not there. It will be weird at first - you will spend more time thinking about writing tests than about writing code. But pretty fast you will develop a sense of what is easy to test and what is not

Collapse
 
jeremyf profile image
Jeremy Friesen

I did that for a brief stint, and it really helped me develop that sense you mention.