Let’s make a game. Better yet, let’s copy one. Let’s implement Mastermind in Ruby.
The living project for this article is on GitHub
First, set up a ruby on your machine using this guide about RVM.
Create an empty class, we’ll call it game.rb. For Ruby, RSpec and TestUnit are popular. We’ll use RSpec for now:
First, install the rspec gem:
Rspec seems happiest when you put all the tests in a spec folder, so let’s go ahead create our test file in spec/game_spec.rb
Write some tests
Here is our first test scenario:
This file tells RSpec
- Load a Ruby object called game from the parent directory
- We are going to test an object called Game
- This test case is called requires an answer
- expect warns RSpec that Game.new will throw an Exception
- We expect this exception to be an ArgumentError.
As long as all your spec files live in a spec directory, you can simply type the following your project directory:
You should see 1 failed test.
Write some code
You’re on your own here. Read up on the Mastermind rules on Wikipedia, then code:
- Write a failing test
- Write the minimal amount of code to fix the test
- Goto 1
- Refactor to clean up as necessary
So, you’re done
You have a working game class. We want to take a moment to save our progress.
Create a git repository
First, convert your project to a local git repository
Push it to github
- Create the repository on GitHub Instructions here
- Add the github repository as the origin remote (See this help page
- Push-pull to sync
Automating the tests
Your work is now saved outside your machine, you can give others access to your repository and collaborate. But with everyone stirring the pot at the same time, how do you know everything’s still working? The answer is continuous integration: we’re going to run a service to run the tests whenever someone checks in code.
Set up a rake task
We need a way to tell a 3rd party how to run the tests on our project. In Ruby, this is called the Rakefile:
This does the following:
- Tell Rake we want to use rspec
- Creates an RSpec task, naming it :spec
- Tells Rake that the default task is our :spec task.
Create and save this file in the project’s directory, and run the following:
You can see that rake starts up RSpec, the output should be the same as running rspec.
Set up a gemfile for Travis
The service we’re going to use, Travis, now knows how it can run our tests. But it has no idea what our ruby environment looks like. We need to create these files in our project directory.
This file tells Travis that we are a ruby project, and we want Ruby 2.0.0
This file tells Travis that we want to use a recent version of RSpec. Create these two files for now, we will send them to GitHub after the next step.
Sign up for Travis
Visit Travis, sign up with your GitHub account and tell it to sync your MasterMind repository.
To tell Travis to make its first build, add the two files we just created using git add and git push them to GitHub.
Watch your inbox.
This can take a few minutes, but eventually Travis will pick up the changes and try to run the tests on their own server. If it worked, you’ll see this reward: