Question

I'm building a class-based Tic-tac-toe game with all the classes in tic_tac_toe.rb. I can load the class into irb for interactive testing with irb -r ./tic_tac_toe.rb, but I have to manually create a player and gameboard instance every time. I included p1 = Player.new int tic_tac_toe.rb but that does not seem to run.

More generally, is what I'm doing a good workflow or not? How should I go about writing some code for my class and testing it and going back? (Is there something simpler than unit testing for this small project?)

Was it helpful?

Solution

To directly address your question, you can simplify your workflow greatly with the addition of RSpec. RSpec is a BDD (behavior driven development) tool for Ruby that will let you describe your classes in an (arguably) more descriptive way than plain jane unit tests. I have included a small code sample below to help get you started.

Create a Gemfile if you do not have one for your project and add RSpec. If you've never done this check out Bundler for more information on Gemfiles.

# in your Gemfile
gem 'rspec'            # rspec testing tool
gem 'require_relative' # allows you to require files with relative paths

Create a spec folder to house your specs (specs are what RSpec calls its tests).

# via Command Line (or in Windows Explorer) create a spec folder in your project
mkdir spec

Create a spec_helper.rb in the spec/ folder to house the configuration for your tests.

# in spec/spec_helper.rb
require "rspec"                   # require rspec testing tool
require_relative '../tic_tac_toe' # require the class to be tested 


config.before(:suite) do
  begin
    #=> code here will run before your entire suite
    @first_player = Player.new
    @second_player = Player.new
  ensure
  end
end

Now that you've setup two players before your test suite runs, you can use these in your tests. Create a spec for your class that you would like to test and suffix it with _spec.

# in spec/player_spec.rb
require 'spec_helper'  # require our setup file and rspec will setup our suite

describe Player do
  before(:each) do
    # runs before each test in this describe block
  end

  it "should have a name" do
    # either of the bottom two will verify player's name is not nil, for example
    @first_player.name.nil? == false
    @first_player.name.should_not be_nil        
  end
end

Run these tests from the root of your project by using bundle exec rspec. This will look for a spec/ folder, load the spec helper, and run your specs. There is much more you can do with RSpec, such as work in Factories etc (this would be for larger projecxts). However for your project you would only need a few specs for your classes.

Other things I would suggest would be RSpec-Given, when you have a firm grasp of rspec. This gem helps DRY up your rspec tests and makes them a bit more readable.

You can also look into Guard and creating a Guardfile, which will watch your files for you and run tests when you change files.

Lastly, I included a small suggestion on a basic project structure to visualize this a bit easier.

/your_project
--- Gemfile
--- tic_tac_toe.rb
--- spec/
------- spec_helper.rb
------- player_spec.rb  

I have linked all the referenced docs so if you have any questions definitely check the links out. The documentation on Bundler, RSpec, RSpec-Given, and Guard is pretty decent. Happy programming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top