We try to "move fast and break things", but that's really possible only when you know what you break. This talk is split into two parts: introduction to why we write tests and a practical demo writing code, for both new codebase and already existing untestable codebase.
During the talk, I'll share multiple strategies with you that will help you cover your untestable code.
After this talk you will be able to start with testing and you will know how to use and mock dependencies to cover any codebase with unit tests.
4. 4
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
5. 5
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
6. 6
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
7. 7
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
8. 8
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
9. 9
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + Easy changes = DELIVER FAST
Huge code example
Million well hidden dependencies
10. 10
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + No fear = JUST SLOWDOWN
Huge code example
Million well hidden dependencies
11. 11
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + No fear = JUST SLOWDOWN
but
100 lines file + No fear = MIGHT BE FASTER
Huge code example
Million well hidden dependencies
12. 12
Different approaches for different problems
Spaghetti, Lasagna, Ravioli can live together
• Different problems require different solutions
• One is not necessarily better than other
• Small script might be just fine as Spaghetti code
• Some framework code can be nice Lasagna
• AWS services are tasty Ravioli
13. 13
Fear slows us down
Regression bugs slow us down
Wrong assumptions slow us down
Manual testing slows us down
Outdated documentation slows us down
Unit tests
what is solves
Unit Tests
Remove fear
Prevent repetitive regression
Validate our assumptions
Are really fast
Document code
Make us faster
15. 15
What is Repository?
It’s not only database
• Repository per Model, not necessary per table
• Provides a substitution for unit test
• Access to Data Store (not necessary SQL) is done only through Repository
- Never from model
- Never ever from controller
- Never ever ever from template
16. 16
Service Pattern
Specific class that does one thing, in our case this is included in models
• Functional Layer / Business Logic
• Unit tests definately for these cases
• Services should be stateless
• Don’t be afraid to introduce new entity/repository
• NO INTERFACES FOR SERVICES