1. Can you TDD Rails?
@andrzejkrzywda
(in love/hate relationship with Rails, since 2004)
Arkency
2. • “What does this code really do?”
• “Can I safely remove it?”
• “I see dead code”
• “I’m afraid to change this code”
• “This code is ugly”
• “Oh, it’s a mock that returns a mock that returns
a mock”
Things developers say
4. • “Where do I start with this feature?”
• “Will I break anything?”
• “I’ll just add a bool parameter here and an if
there”
• “Changing this will break dozens of tests”
7. • “What does this code really do?”
• “Can I safely remove it?”
• “I see dead code”
• “I’m afraid to change this code”
• “This code is ugly”
• “Oh, it’s a mock that returns a mock that returns
a mock”
Things developers say
8. • “Where do I start with this feature?”
• “Will I break anything?”
• “I’ll just add a bool parameter here and an if
there”
• “Changing this will break dozens of tests”
28. TDD hard skills
• tools - rspec, xunit, guard
• automatic run
• full suite vs partial suite
• TDD JavaScript tools
• IDEs
29. TDD soft skills
• spikes
• GTD
• Continuous delivery (commit after every cycle)
• triangulation
• isolate from the framework (aka the Rails sandwich)
• tests refactoring
• when to mock?
30. Spikes
• “It’s not clear to me, what I need to do”
• a prototype
• throw-away code
• cowboy hacking
• timeboxed
• make notes
• can be temporarily deployed, if it’s “secure” just to feel it
31. Getting Things Done
• “I know what to do, now just do it”
• checklists
• Pomodoro
• procrastination
32. Continuous Delivery
• Commit/push after every red/green/refactor
cycle
• push == deploy
• small steps
• don’t break the existing code
• always green
33. TDD triangulation
• the simplest possible test
• make it green in the stupid way
• next test
• implement with an ‘if’
• next test
• refactor and implement the right way
34. Framework isolation
• controller? - extract service object or a query
object, as early as possible
• model? - move logic to service object
• no need to test ActiveRecord
• extract repository object
• render views with locals
35. tests refactoring
• test is code too
• make it short, elegant, easy to read
• apply Clean Code rules
• feel free to remove some tests
38. exercising TDD
• code kata
• YouTube: TDD
• extract business logic as a gem, TDD that
• “domain spike” - what if all objects can live in
memory (no db) and there’s no boundaries (http,
api), just objects
• 15 minutes a day
39. It’s all up to you to
learn how to TDD Rails
Thanks!
http://rails-refactoring.com/