…and why is it (often) not a single class
What’s the “unit” in unit testing
Dennis Doomen
About Me
Hands-on architect in the .NET space with 25 years of experience on
an everlasting quest for knowledge to build the right software the right
way at the right time
Why is this important?
Because we like…
…code that can be changed by any
developer in the team without fear and
with confidence
Test Driven
Development in
reality
Design class
responsibilities
Write first test
Generate stubs
Fail for the right
reason
Implement
real deal
Ensure test
succeeds
Identify
alternative
scenarios
Repeat twice
Refactor
Pros and cons of
different levels of
testing
Exploratory
Testing
Browser
Testing
HTTP/API Tests
Component/Integration Tests
“Unit” Tests
Scope,
Execution
Time
and
flakiness
Simplicity
&
Ease
of
debugging
Some examples
Smells & heuristics to find the right scope
Designed for
reusability? Then
tested separately
Is it an
implementation
detail? Then don’t
test it directly
Is the test really
testing the real
surface area?
Does refactoring
require rewriting the
tests?
Things in
adjacent folders
usually mean
they are separate
Returning mocks
from mocks is often
a sign of a scope
issue
Tips to improve testability
Replace ugly
dependencies with
DIP
It’s fine to include
the database in tests
It’s fine to test
smaller when it adds
value
Don't bother
arguing about “unit”
vs “integration”
Organize code by
functional
boundaries
Apply DRY within
those boundaries
Mock the
dependencies
outside of the
scope
A less trivial example
Command Handlers
Commands
Domain Model
Event Store
Events
App
Read Data
RavenDB
Projectors
Events
HTTP API
Projections
Events
Test Fixture
In-Memory
Event Store
Read
Data
RavenDB
Projectors
Events
HTTP API
Projections
Unit Test
HTTP request
Test Data
Sources: https://tinyurl.com/4sukm55n
Ask a great questions
…and win a Jetbrains 1-year license
…ask at fluentassertions.slack.com
…ping me at @ddoomen
…email me at dennis.doomen@avivasolutions.nl

What is the right unit in unit testing (UpdateConf 2022)