tdd with rails

119
TDD with Rails AndrzejKrzywda.com RuPy 2008 Poznan, Poland

Upload: andrzej-krzywda

Post on 13-Jan-2015

6.553 views

Category:

Technology


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: TDD with Rails

TDD with RailsAndrzejKrzywda.com

RuPy 2008Poznan, Poland

Page 2: TDD with Rails

Who am I?

Page 3: TDD with Rails

Andrzej Krzywda

• Developing software since 2000

• Java/.NET/PHP/Python/Rails

Page 4: TDD with Rails

Now mostly Rails

Page 5: TDD with Rails

First Rails app in November 2004

Page 6: TDD with Rails

I started doing TDD in 2001

Page 7: TDD with Rails

Also involved in a project called Resolver

One, written in IronPython

Page 8: TDD with Rails

Started a Rails company in 2007

Page 9: TDD with Rails

Test Driven Development

Page 10: TDD with Rails

The goals in a software project

Page 11: TDD with Rails

Requirements are goals

Page 12: TDD with Rails

What helps in achieving goals?

Page 13: TDD with Rails

Goal visualization

Page 14: TDD with Rails

Software requirements

Page 15: TDD with Rails

TDD (in a high level) is about visualizing goals.

Page 16: TDD with Rails

Software developers are responsible

for visualization of other people goals

Page 17: TDD with Rails

What is TDD?

Page 18: TDD with Rails

TDD is about writing tests BEFORE writing

production code.

Page 19: TDD with Rails

What kind of tests?

Page 20: TDD with Rails

Be pragmatic.

Page 21: TDD with Rails

TDD definitions

Page 22: TDD with Rails

TDD is like politics or religion

Page 23: TDD with Rails

Everyone thinks he’s right

Page 24: TDD with Rails

I think they are wrong...

Page 25: TDD with Rails

My TDD

Page 26: TDD with Rails

Acceptance tests

+Unit tests

Page 27: TDD with Rails

Acceptance tests + Unit tests

Page 28: TDD with Rails

Definitions

Page 29: TDD with Rails

Acceptance test is a test from the user’s

perspective.

Page 30: TDD with Rails

Unit test is a test for a class.

Page 31: TDD with Rails

Which are more important?

Page 32: TDD with Rails

It depends.

Page 33: TDD with Rails

0

25

50

75

100

2001 2003 2006 2008

Unit tests Acceptance tests

The level of test coverage

Java IronPython Rails

Page 34: TDD with Rails

Why?

Page 35: TDD with Rails

Why

• Rails is a more mature community (in the TDD aspect)

• So many great tools

• REST! - almost no need to test controllers

• Almost only model unit tests

• Acceptance tests, trivial with RSpec Stories + Webrat

Page 36: TDD with Rails

Acceptance tests

Page 37: TDD with Rails

Doing TDD means to have many tests.

Page 38: TDD with Rails

TDD is a good requirements

management technique

Page 39: TDD with Rails

TDD

• 3 x D rule

• Defense (Tests)

• Design

• Documentation

Page 40: TDD with Rails

???

Page 41: TDD with Rails

Let’s take an example.

Page 42: TDD with Rails

A blog application

Page 43: TDD with Rails

We are about to add a ‘commenting’ feature.

Page 44: TDD with Rails

How can we visualize it?

Page 45: TDD with Rails

As a Rails developer?

Page 46: TDD with Rails

> script/generate migration CreateCommentsTable content:text

class Post < ActiveRecord::Base has_many :commentsend

class Comment < ActiveRecord::Base belongs_to :postend

<%= render_collection @post.comments %>

Page 47: TDD with Rails

As a software developer?

Page 48: TDD with Rails

$$$$

Page 49: TDD with Rails

Someone pays for your work, right?

Page 50: TDD with Rails

What users are involved in the blog commenting

feature?

Page 51: TDD with Rails

A blog reader

Page 52: TDD with Rails

As a blog reader

I want to comment on a blog post

So that I can share my thoughts on the topic

Page 53: TDD with Rails

A blog owner / author

Page 54: TDD with Rails

As a blog ownerI want to display

commentsSo that other people can see how cool my

blog is.

Page 55: TDD with Rails

As a blog ownerI want to moderate

commentsSo that there is no spam

Page 56: TDD with Rails

As a blog ownerI want to display captcha

imagesSo that there is no spam

Page 57: TDD with Rails

Another blog reader

Page 58: TDD with Rails

As another blog readerI want to reply to other

commentsSo that I can prove they

are wrong

Page 59: TDD with Rails

As an RSS subscriber...

Page 60: TDD with Rails

As a bad person...

Page 61: TDD with Rails

As a bad personI want to use SQL InjectionSo that I can break your blog and become famous.

Page 62: TDD with Rails

The structure

Page 63: TDD with Rails

Spikes / prototyping

Page 64: TDD with Rails

User stories

Page 65: TDD with Rails

Show your customer the user stories.

Page 66: TDD with Rails

Discuss it.

Page 67: TDD with Rails

Estimate it.

Page 68: TDD with Rails

Prioritize.

Page 69: TDD with Rails

Implement it.

Page 70: TDD with Rails

Split it into tasks which are usually related to

scenarios.

Page 71: TDD with Rails

TDD helps you focusing on one task at a time.

Page 72: TDD with Rails

There’s always exactly one test failing.

Page 73: TDD with Rails

Tools

Page 74: TDD with Rails

RSpec storiesWebrat

Page 75: TDD with Rails

Selenium RCWatir

Page 76: TDD with Rails

RSpec / BDD

Page 77: TDD with Rails

RSpec stories != RSpec examples

Page 78: TDD with Rails

RSpec stories is for acceptance tests

Page 79: TDD with Rails

‘RSpec examples’ is a unit testing tool.

Page 80: TDD with Rails

The process

Page 81: TDD with Rails

List the scenarios.

Page 82: TDD with Rails

RSpec storyStory: Commenting on a blog

As a blog reader I want to comment So that I can share my thoughts about the article Scenario: Simple adding of a comment

Given an existing blog post And a blog reader writing a comment When he submits it Then it appears below the blog post

stories/comments

Page 83: TDD with Rails

require File.join('stories', 'helper')

steps_for :comments doend

with_steps_for :comments do run 'stories/comments', :type => RailsStoryend

Page 84: TDD with Rails

require File.join('stories', 'helper')

steps_for :comments do Given "an existing blog post" do @post = Post.create(:title => 'title', :body => 'body') endend

with_steps_for :comments do run 'stories/comments', :type => RailsStoryend

Page 85: TDD with Rails

require File.join('stories', 'helper')

steps_for :comments do Given "an existing blog post" do @post = Post.create(:title => 'title', :body => 'body') end Given "a blog reader writing a comment" do visits '/' clicks_link 'Read more...' fills_in 'comment[content]', :with => 'First!' endend

Page 86: TDD with Rails

RSpec story runnerrequire File.join('stories', 'helper')

steps_for :comments do Given "an existing blog post" do @post = Post.create(:title => 'title', :body => 'body') end Given "a blog reader writing a comment" do visits '/' clicks_link 'Read more...' fills_in 'comment[content]', :with => 'First!' end

stories/comments.rb

Page 87: TDD with Rails

RSpec story runner When "he submits it" do clicks_button "Add" end Then "it appears below the blog post" do visits '/' clicks_link 'Read more...' response.body.should have_text 'First!' end

end with_steps_for :comments, :posts do run 'stories/comments', :type => RailsStoryend

stories/comments.rb

Page 88: TDD with Rails

Run the test

Page 89: TDD with Rails

It fails

Page 90: TDD with Rails

Implement one step at a time

Page 91: TDD with Rails

If it’s not trivial start with a spec first.

Page 92: TDD with Rails

RSpec example

Page 93: TDD with Rails

• Bryan Helmkamp

• github.com/brynary/webrat

• manipulates DOM

• builds POST from DOM

Webrat

Page 94: TDD with Rails

Problems?

Page 95: TDD with Rails

Slow builds

Page 96: TDD with Rails

Fragile, unreliable tests

Page 97: TDD with Rails

What about the scientists opinions?

Page 98: TDD with Rails

Thirumalesh Bhat and Nachiappan Nagappan. Evaluating the efficacy of test- driven development: industrial case studies. In ISESE ’06: Proceedings of the 2006 ACM/IEEE international symposium on Empirical software engineering, pa- ges 356–363, New York, NY, USA, 2006. ACM.

Gerardo Canfora, Aniello Cimitile, Felix Garcia, Mario Piattini, and Corrado Aaron Visaggio. Evaluating advantages of test driven development: a controlled experi- ment with professionals. In ISESE ’06: Proceedings of the 2006 ACM/IEEE inter- national symposium on Empirical software engineering, pages 364–371, New York, NY, USA, 2006. ACM.

Steven Fraser, Dave Astels, Kent Beck, Barry Boehm, John McGregor, James New- kirk, and Charlie Poole. Discipline and practices of tdd: (test driven development). In OOPSLA ’03: Companion of the 18th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, pages 268–270, New York, NY, USA, 2003. ACM.

Ivana Turnu, Marco Melis, Alessandra Cau, Michele Marchesi, and Alessio Setzu. Introducing tdd on a free libre open source software pro ject: a simulation expe- riment. In QUTE-SWAP ’04: Proceedings of the 2004 workshop on Quantitative techniques for software agile process, pages 59–65, New York, NY, USA, 2004. ACM.

Page 99: TDD with Rails

What are the consequences of doing

TDD?

Page 100: TDD with Rails

I don’t use debuggers.

Page 101: TDD with Rails

Simpler, thus better design

Page 102: TDD with Rails

Frequently asking the question:

“How would I test it?”

Page 103: TDD with Rails

After many years of doing TDD, I find it

VERY difficult to work with untested code.

Page 104: TDD with Rails

Unit testing tips

• One assertion per test method

• Write the assertion first

• Thin controllers, fat models

• REST

• Fixtures, it’s easier without them

• Specs output as a documentation

Page 105: TDD with Rails

Quotes

Page 106: TDD with Rails

“TDD was the single most effective software

development habit I adopted”

Page 107: TDD with Rails

“My impression is that it's intuitive the way riding a bicycle is intuitive. Extremely UN-intuitive for the child who is learning, but to the adult hardly anything but common sense and difficult to unlearn.

That said, as a developer who's fallen down and scraped his knee... since I need to get from point A to point B fast, I know how to run already so I still do that.”

Page 108: TDD with Rails

“TDD doesn't work very well when you're

trying to work on something new - try a

spike instead, and follow with test-driven coding

later”

Page 109: TDD with Rails

“It’s not clear how to split tests into suites”

Page 110: TDD with Rails

“TDD is intuitive once you start doing it”

Page 111: TDD with Rails

“You have to start thinking in a different

way”

Page 112: TDD with Rails

“TDD is only one thing, don’t forget about the

others: refactoring, code quality”

Page 113: TDD with Rails

“Writing good tests may be difficult for

beginners”

Page 114: TDD with Rails

“I can’t achieve a 100% code coverage”

Page 115: TDD with Rails

My thoughts

Page 116: TDD with Rails

A different way of thinking

Page 117: TDD with Rails

I always think “How can I test that” instead of

“how do I implement it”

Page 118: TDD with Rails

Live coding

Page 119: TDD with Rails

Thank you!

• http://AndrzejKrzywda.com

• Rails consulting, training

• Feel free to email/IM me with TDD questions

[email protected]