Code Monkey home page Code Monkey logo

tdd-roman-numerals's Introduction

TDD Roman Numerals

Objectives
Configure a project to use RSpec
Explore the methodology of Test Driven Development
Write expectations that test method behavior
Use the Red, Green, Refactor pattern

In today's lab we will be creating a Converter that takes in a base 10 integer and returns a Roman Numeral string:

Input Output
1 "I"
2 "II"
3 "III"
4 "IV"
5 "V"
6 "VI"
7 "VII"
8 "VIII"
9 "IX"
10 "X"

To get started, let's watch a ~10 minute demonstration of Test Driven Development (TDD): Roman Numeral Kata.

What's surprising about the speakers approach to writing code and passing tests? (Pay special attention to the way he cheats!)

Test Driven Development

Test Driven Development can be thought of as a 3 step processs described as "Red, Green, Refactor":

  1. Write the test first. Make sure the expectation is failing. ("red")
    • Why? Because if the test passes right away we have a "false positive".
  2. Write code to pass the test ("green")
    • It's common to break existing features when you add a new feature. Make sure that all of your tests are passing!
  3. Stop and cleanup ("refactor")
    • Is your code DRY? Are your tests DRY? As you make changes, run your tests frequently to ensure your code still works.

When pair programming one strategy is to play "ping pong". One person writes the test, the other person writes code to pass it. Then you switch roles.

Setup

Before you can start writing tests, you need to setup the RSpec test suite in your project. Please follow the official setup instructions.

At a minimum, you will need to install and configure the following gems: rspec, rspec-core, rspec-expectations. (That means you should have a Gemfile!).

Your tests or "specs" will live in a /specs folder. Since we are testing our Converter class which lives in converter.rb, we will name our spec "/specs/converter_spec.rb".

Make sure you are in the root of your project when you run the rspec command!

Writing Expectations

You will need to familiarize yourself with the "built-in matchers" included in the rspec-expectations library:

Bonus

Want anther challenge? Try creating a "numbers to words" converter. It should accept an integer and convert it into english words:

Input Output
1 "one"
10 "ten"
21 "twenty one"
101 "one hundred and one"

Resources

tdd-roman-numerals's People

Contributors

ben-manning avatar nathanallen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.