Note: This project is currently in a work in progress so not all languages or sections are complete.

Test Driven Development with Ruby

Installing Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and run the following commands:

1
2
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Ruby

Now we can easily install Ruby with Homebrew using the following:

1
brew install ruby

Creating the Project

Now we'll use Bundler to initialize a project for us.

1
2
3
gem install bundler
bundle gem ubyray --no-coc --no-mit
cd ubyray

Dependency Management

When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.

For Ruby we'll use the tool Bundler and our dependencies will be defined in the file Gemfile.

1
# We already installed Bundler above

Adding a Testing Library

We're going to use the testing library RSpec to write our tests.

Edit the file Gemfile and add RSpec as a testing dependency:

1
spec.add_development_dependency 'rspec'

Then we retrieve RSpec by running bundle.

Finally we initialize RSpec by running rspec --init.

The First Test

Create the file spec/piglatin_spec.rb with the following contents:

1
2
3
4
5
6
7
describe Piglatin do
  context "word starting with a vowel" do
    it "translates the word" do
      expect(Piglatin.translate_word "apple").to eq "appleyay"
    end
  end
end

Then run the test with the command rspec to see the results:

1
...spec/piglatin_spec.rb:1:in `<top (required)>': uninitialized constant Piglatin (NameError)

Comments