Skip to content
Dave Strus edited this page Feb 8, 2016 · 1 revision

MutantAPIModel

  • Inheritance
  • The splat operator (*)
  • method_missing
  • More unit testing with minitest
  • Stubbing with mocha
  • Factory pattern
  • Template Method pattern

Create a gem

Create a new gem...

$ bundle gem mutant_school_api_model
$ cd mutant_school_api_model

Update the .gemspec file.

Edit the .gemspec file.

  • Update spec.version to have "API" in all caps.
  • Enter a summary and description.
  • Update the bin directory in spec.bindir and spec.executables.
  • Add pry and minitest-reporters as development dependencies.

minitest-reporters - Create customizable MiniTest output formats.

  • Add http as a regular dependency.

HTTP (The Gem! a.k.a. http.rb) is a fast Ruby HTTP client with a chainable API, streaming support, and timeouts mutant_school_api_model.gemspec

spec.version       = MutantSchoolAPIModel::VERSION
spec.summary       = 'Mutant School API wrapper'
spec.description   = 'Mutant School API wrapper for use in automated tests'

# ...

spec.bindir        = "bin"
spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }

# ...

spec.add_development_dependency 'pry', '~> 0.10'
spec.add_development_dependency 'minitest-reporters', '~> 1.1'

spec.add_dependency 'http', '~> 1.0'

Update the main module.

Add a few requires in lib/mutant_school_api_model.rb:

require 'http'
require 'json'
require 'pry'

Rename the module in lib/mutant_school_api_model.rb and lib/mutant_school_api_model/version.rb to have "API" in all caps.

module MutantSchoolAPIModel

Update the bin/console script.

Update the bin/console script to use pry instead of irb. bin/console

#!/usr/bin/env ruby

require "bundler/setup"
require "mutant_school_api_model"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "pry"
Pry.start

Use spec in place of test in file names.

  • Rename test to spec.
  • Rename spec/mutant_school_api_model_test.rb to spec/mutant_school_api_model_spec.rb

Rewrite the existing test in declarative style. spec/mutant_school_api_model_spec.rb

require 'test_helper'

describe MutantSchoolApiModel do
  it 'has a version number' do
    _(::MutantSchoolApiModel::VERSION).wont_be_nil
  end

  it 'does something useful' do
    _(false).must_be_same_as true
  end
end

Update the Rakefile to look for spec files. Rakefile

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
  t.libs << "spec"
  t.libs << "lib"
  t.test_files = FileList['spec/**/*_spec.rb']
end

task :default => :spec

Use Minitest::Reporters

Update spec/test_helper.rb to use Minitest::Reporters.

require "minitest/reporters"
Minitest::Reporters.use!

Run the tests. One should pass, and the other should fail.