Can you TDD Rails?

39
Can you TDD Rails? @andrzejkrzywda (in love/hate relationship with Rails, since 2004) Arkency

description

What it takes to do TDD with Rails? Hard skills vs soft skills. It's up to you to improve.

Transcript of Can you TDD Rails?

Page 1: Can you TDD Rails?

Can you TDD Rails?

@andrzejkrzywda (in love/hate relationship with Rails, since 2004)

Arkency

Page 2: Can you TDD Rails?

• “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

Page 3: Can you TDD Rails?

Fear/hate of existing code

Page 4: Can you TDD Rails?

• “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”

Page 5: Can you TDD Rails?

Fear of new code

Page 6: Can you TDD Rails?

hypothesis: !

TDD can help us with those problems

Page 7: Can you TDD Rails?

• “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

Page 8: Can you TDD Rails?

• “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”

Page 9: Can you TDD Rails?

Fear-driven coding is not fun

Page 10: Can you TDD Rails?

Fear-driven improvement is

fun

Page 11: Can you TDD Rails?

Skills

Page 12: Can you TDD Rails?

Hard skills vs

soft skills

Page 13: Can you TDD Rails?

Hard skills !

where computers excel

Page 14: Can you TDD Rails?

Soft skills !

where humans excel

Page 15: Can you TDD Rails?

Chess - hard skills

• endings

• openings (theory)

• realizing advantage

• tactics (combinations)

Page 16: Can you TDD Rails?

Chess - soft skills

• recognize patterns

• understand nuances

• strategy

• plans

Page 17: Can you TDD Rails?

In a chess game (40-50 moves) we’ve got about 4-5 critical positions that require more

thinking

Page 18: Can you TDD Rails?

Rails

Page 19: Can you TDD Rails?

Rails - Hard skills• Rails Controllers API

• Rails views API

• ActiveRecord API

• CoffeeScript/JavaScript

• Ruby stdlib

• refactoring techniques

Page 20: Can you TDD Rails?

Rails - soft skills• understand requirements

• implement business rules

• applying refactoring

• splitting modules / services

• isolating from Rails

• isolating from Gems

Page 21: Can you TDD Rails?

TDD

Page 22: Can you TDD Rails?

TDD == TestFirst + Refactoring

Page 23: Can you TDD Rails?

Red Green

Refactor

Page 24: Can you TDD Rails?

“but TDD is hard with Rails”

Page 25: Can you TDD Rails?

The TDD drama !

“If TDD with Rails is hard, then something is wrong with TDD.”

Page 26: Can you TDD Rails?

TDD is fine Rails is fine

!

Most likely, your code is not fine

Page 27: Can you TDD Rails?

TDD skills

Page 28: Can you TDD Rails?

TDD hard skills• tools - rspec, xunit, guard

• automatic run

• full suite vs partial suite

• TDD JavaScript tools

• IDEs

Page 29: Can you TDD Rails?

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?

Page 30: Can you TDD Rails?

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

Page 31: Can you TDD Rails?

Getting Things Done

• “I know what to do, now just do it”

• checklists

• Pomodoro

• procrastination

Page 32: Can you TDD Rails?

Continuous Delivery• Commit/push after every red/green/refactor

cycle

• push == deploy

• small steps

• don’t break the existing code

• always green

Page 33: Can you TDD Rails?

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

Page 34: Can you TDD Rails?

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

Page 35: Can you TDD Rails?

tests refactoring

• test is code too

• make it short, elegant, easy to read

• apply Clean Code rules

• feel free to remove some tests

Page 36: Can you TDD Rails?

when to mock?

• boundaries

• db access

Page 37: Can you TDD Rails?

How can I improve at TDD?

Page 38: Can you TDD Rails?

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

Page 39: Can you TDD Rails?

It’s all up to you to learn how to TDD Rails

Thanks!http://rails-refactoring.com/