can you tdd rails?

Post on 11-May-2015

1.719 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Can you TDD Rails?

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

Arkency

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

Fear/hate of existing code

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

Fear of new code

hypothesis: !

TDD can help us with those problems

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

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

Fear-driven coding is not fun

Fear-driven improvement is

fun

Skills

Hard skills vs

soft skills

Hard skills !

where computers excel

Soft skills !

where humans excel

Chess - hard skills

• endings

• openings (theory)

• realizing advantage

• tactics (combinations)

Chess - soft skills

• recognize patterns

• understand nuances

• strategy

• plans

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

thinking

Rails

Rails - Hard skills• Rails Controllers API

• Rails views API

• ActiveRecord API

• CoffeeScript/JavaScript

• Ruby stdlib

• refactoring techniques

Rails - soft skills• understand requirements

• implement business rules

• applying refactoring

• splitting modules / services

• isolating from Rails

• isolating from Gems

TDD

TDD == TestFirst + Refactoring

Red Green

Refactor

“but TDD is hard with Rails”

The TDD drama !

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

TDD is fine Rails is fine

!

Most likely, your code is not fine

TDD skills

TDD hard skills• tools - rspec, xunit, guard

• automatic run

• full suite vs partial suite

• TDD JavaScript tools

• IDEs

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?

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

Getting Things Done

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

• checklists

• Pomodoro

• procrastination

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

cycle

• push == deploy

• small steps

• don’t break the existing code

• always green

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

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

tests refactoring

• test is code too

• make it short, elegant, easy to read

• apply Clean Code rules

• feel free to remove some tests

when to mock?

• boundaries

• db access

How can I improve at TDD?

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

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

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

top related