tdd!

15
WTF is TDD A journey to writing awesome code, one test at a time

Upload: steven-nunez

Post on 06-May-2015

246 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tdd!

WTF is TDDA journey to writing awesome code, one test at a time

Page 2: Tdd!

WTF we'll be covering● What is testing, Why test, and How testing

will make you a better person● What is TDD, why do it, and why it's better

than writing tests AFTER you've written your code

● Go over a code example built out with TDD

Page 3: Tdd!

Who TF is this guy?● Steven Nunez (@_StevenNunez)

○ Junior Developer at Cyrus Innovation■ AWESOME PLACE! Premier Dev Shop

● Agile/TDD All day, E'ryday○ Organizer of Unearth Ruby

■ http://www.meetup.com/unearthruby/○ Co-Organizer of NYC Rubyist Roundtable

■ http://www.meetup.com/nycruby/

Page 4: Tdd!

Stuff you need to F'ing knowHow to read this:class Order attr_reader :items

def initialize @items = Hash.new(0) end

def add(item) items[item] += 1 end end

Page 5: Tdd!

The Act of isolating parts of your code with a known state to ensure it works as expected.

Setup follows AAA principle:

Arrange - Put your code in some stateAct - Cause its state to change (or not)Assert - Verify your getting what you expect

What Is Testing

Page 6: Tdd!

Types of testing● End to end tests/ Acceptance tests

○ Here there be Cucumbers. Capybara, Webrat○ Used to ensure system works as a whole

● Unit Tests○ Rspec, Test::Unit, Minitest○ View tests○ Controller tests○ Model tests

We'll be covering Unit Tests

Page 7: Tdd!

● Lets you know when things break○ You'd be surprised how code works (or doesn't)

● Allows for courageous refactoring● Gives documentation on how your code is

SUPPOSED to work. Learn to expect tests.● It is your duty to deliver working code. The

only way to ensure you've done your job is for it to be verified somehow.

Why Test

Page 8: Tdd!

Testing basicsWe'll be using Rspec...http://rspec.info/https://github.com/rspec/rspec-expectations#built-in-matchers

Codey Code! ----> http://bit.ly/PUwQLf

Page 9: Tdd!

What is Test Driven Development?

Page 10: Tdd!

Why do TDD?● Forces you to think small

○ You have a clear objective● Less thoughts to juggle

○ All you know is how you want it to work● Leads to modular code

○ Each part of your code does one job● Write less code● Increase documentation ● It makes you happy!

○ Red, Green, REFACTOR!

http://edu.mkrecny.com/thoughts/be-nice-to-programmers

Page 11: Tdd!

How to TDDRed Green Refactor 1. Write a test.2. Watch it fail.3. Write code to pass your test.4. Look for ways to make your code more

expressive5. GOTO 1

Page 12: Tdd!

Our taskAvi, in his infinite wisdom has figured that this tech bubble is about to burst and really wants to prepare us for a long healthy life in Fast Food. His newest venture: Avi's Ovulating Avians, is a boutique breakfast shop that plans to disrupt the food consumption market in a big way.He wants to leverage our algorithm wrangling skills to write a food matching meal recommendation engine to make upselling our products to our unsuspecting patrons easier.What do we need for version 0.1?We need a way to capture customer orders, and inspect the order's ingredients. We'll then need to make recommendations based on those ingredients.

Things we're buildingA Menu with Menu Items in it. The Items need to have a name, price, calorie count, and ingredientsOrders consist of a menu item, and a quantity.Orders can calculate their totalRecommender can recommend Menu Items based on the ingredients in a menu item.

https://github.com/StevenNunez/FlatironDiner

Page 13: Tdd!

Questions!

Page 14: Tdd!

Resourceshttps://www.relishapp.com/rspechttp://pragprog.com/book/achbd/the-rspec-bookhttp://www.codeschool.com/courses/testing-with-rspec

Practical Object Oriented Design by Sandi Metzhttp://www.poodr.info/

Page 15: Tdd!

THANKS!