TDD Workshop Notes
Twitter hashtag #tddberlin
Mobile Central Europe Conference in Warsaw in Feb 2015
book Michael Feathers - Working With Legacy Code
- Specta https://github.com/specta/specta
- xcpretty - simplify xcodebuild output
- xctool - similar to xcpretty. slower than xcpretty. does not support incremental builds.
- AppCode - less typing
- OCMockito https://github.com/jonreid/OCMockito
- OCMock http://ocmock.org
- KIF https://github.com/kif-framework/KIF
Introduction to tests and testing framework
Pawel Dudek @eldudi
Unit test life cycle
Arrange - set up
Act - do something
Assert - check result
What a unit test is not
Read book Michael Feathers - Working With Legacy Code
- talks to database, filesystem, network
- edit config files or environment
Property of a good unit test
Has to be FAST!
Write test first!
Gets you into the zone
Helps track work
Use your own API before someone else or the rest of the app
If it's hard to test, it's hard to use.
Reduce amount of QA and testing. You will still have bugs.
Work from the outside in
Use examples to clarify requirements
Describe and context block
beforeEach - Arrange and Act
it - Assert
afterEarch - destroy created objects and state from beforeEach
Separation into beforeEach and it
run tests automatically on each change of a file
xcpretty - simplify xcodebuild output to one . per test. builds faster
What does Specta do, that XCTest does not?
Red Green Factor
Aleksader Zubala @alekzubala
develop in short repeatable cycles
form hypothesis and check them
confidence to refactor
improve code quality
What will move the project forward?
- Write test (object and method might not exist). AppCode makes it easy to create missing files, methods
- Test Fails
Solution does not have to be the best one. Even hardcoding is ok.
Make test pass. Test the test.
Improve solution. Acutally implement.
DRY - don't repeat yourself
Red, Green, Refactor
What do you get
spend most time on refactoring
easy to identify what goes wrong
move quickly through red, green phases
make cycles short
don't forget refactoring
Add correct index when opening swiping preview.
Types of Test
Return test, value test
When faking, mocking objects you might have to create categories. Only add those to the test target.
describe and context is used to structure the tests
How to work with legacy code effectively
Maciej Oczko @maciejoczko
Creating a new spec file
File, new - File (not new class) - ClassToTestSpec.m
Add to to test target
type cspec + tab to create starting test code
- test if rightBarButtonItem changes correctly
Testing View related methods
View lifecycle methods (viewDidLoad etc.) are not called automatically. You have to call them yourself.
Be careful: calling -[UIViewcontroller view] will call loadView, viewDidLoad. But will not call viewWillAppear.
Find inflection point: narrow interface to a set of classes
example here: Singleton PollManager made testing difficult
- break external dependencies with dependency injection (passing parameters e.g. initWith:)
- break internal dependencies with extracting a small method
After that make changes.
extract validation logic to test textfiel validation. lines 116-138
cit is live template (code snippet) that expands. See Settings -> Live templates
alt+enter - create class, create missing methods etc.
alt+cmd+c - create constants from repeated strings
cmd+shift+t - switch from test to implementation class
alt+enter - dow what i need, like create new class